如果给出电子邮件ID,则无法通过jquery val()函数从电子邮件字段获取值

问题描述 投票:0回答:2

我正在开发一个codeigniter应用程序。我的问题是,当我在html5电子邮件输入中提供电子邮件ID时,jquery val()函数无法访问它,但当我提供除电子邮件ID格式之外的任何其他值时,可以访问它。 这是我的HTML,

<html>
    <head>
        <title>
                Personal Cash Book
        </title>
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/css/bootstrap.min.css">
        <link rel="stylesheet" href="<?php echo base_url();?>assets/CSS/login.css">
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    </head>

    <body>


                    <div class="bg login">
                        <div class="container">
                            <div class="row">
                                <div class="col-md-9 col-md-push-3">
                                    <div class="page-header">
                                        <h3>
                                            <b>
                                                Personal Cash Book
                                            </b>
                                        </h3>


                                    </div>

                                        <form>
                                        <div class="form-group">
                                            <label class="form-label" for="username">
                                                USERNAME
                                            </label>
                                            <input type="email" id="username" name="email" class="form-control"  required >
                                        </div>


                                        <div class="form-group">
                                            <button type="button" id="sub" class="btn btn-danger">
                                                Submit
                                            </button>


                                        </div>
                                    </form>
                                        <h3>
                                            <?php echo $this->session->flashdata('message');?>
                                        </h3>



                                </div>
                            </div>


                        </div>

                    </div>

                    <div class="container footer-section">
                        <div class="row">
                            <div class="col-md-12">
                                <footer >
                        All rights reserved.Created By&nbsp; <a href="http://www.christalinfotech.com" class="author" target="_blank"> Bibin Paul.P</a>&nbsp;&nbsp; Licenced to Sido Antony
                    </footer>
                            </div>
                        </div>

                    </div>

                    <script>
                        $(document).ready(function()
                        {
                            $(document).on('click','#sub',function()
                            {
                                var email=$('#username').val();
                                $.post('<?php echo base_url();?>master/reset-password/'+email+'/',function(data)
                                    {
                                        alert(data);
                                    }
                                );
                            })
                        })
                    </script>


    </body>
</html>

这是我的控制者,

<?php

class Master extends CI_Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->load->library('encryption');
    }

    public function index()
    {
        $this->load->view('login');
    }

    public function login()
    {
        $this->form_validation->set_rules('email','USERNAME','xss_clean|required|valid_email');
        $this->form_validation->set_rules('password','PASSWORD','xss_clean|required');
        $this->form_validation->set_error_delimiters('','');
        if($this->form_validation->run()===FALSE)
        {
            if(form_error('email'))
            {
                $this->session->set_flashdata('message',form_error('email'));
            }

            else
            {
                $this->session->set_flashdata('message',form_error('password'));
            }

            $this->load->view('login');
        }

        else
        {
            $email=$this->input->post('email');
            $password=$this->input->post('password');

            $data=$this->common->login($email,$password);
            if($data)
            {
                $this->session->set_userdata('user_data',$data);
                redirect('/dashboard');
            }

            else
            {
                $this->session->set_flashdata('message','Incorrect credentials');
                redirect('/');
            }

        }
    }

    public function logout()
    {
        $this->session->unset_userdata('user_data');
        $this->session->set_flashdata('message','You are logged out successfully');
        redirect('/');
    }

    public function forget_password()
    {
        $this->load->view('forget_password');
    }

    public function reset_password($email)
    {
        echo "password reset".$email;
    }
, 

?>

目的是我想检查用户名是否存在我使用jquery脚本实现它作为启用csrf保护时codeigniter不支持的ajax函数。我使用html5电子邮件输入字段输入用户名,它的id是用户名。当我提供除电子邮件ID之外的值时,我可以从服务器端获得响应,但是当我给出电子邮件ID时没有响应。这是我的问题

jquery html5 codeigniter
2个回答
0
投票

这条线是否有可能干扰id =“username”?

<label class="form-label" for="username">

不确定jquery是否正在获取没有值的用户名的第一个实例


0
投票

这是您的脚本测试和工作正常:

<script>
    $(document).ready(function() {
        $(document).on('click','#sub', function() {
            var email = $('#username').val();
            $.get('<?php echo base_url('welcome');?>/test?email='+email, function(data) {
                alert(data);
            });
        });
    });
</script>

没有其他任何改变,它正确地获得了电子邮件的价值。

© www.soinside.com 2019 - 2024. All rights reserved.