jquery - 如果给定电子邮件 ID,则无法通过 jquery val() 函数从电子邮件字段中获取值
问题描述
我正在开发一个 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 <a href="http://www.christalinfotech.com" class="author" target="_blank"> Bibin Paul.P</a> 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 时没有得到响应。这是我的问题
解决方案
这是您的脚本经过测试并且工作正常:
<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>
没有其他任何更改,它正在正确获取电子邮件的价值。
推荐阅读
- flutter - 如何让 Flutter 应用使用某个网络证书?
- docker - 如何将'-search.maxUniqueTimeseriesi'标志传递给victoriametrics?
- reactjs - 通过配置和功能进行映射 - React
- kubernetes - 获取 HPA 的未知目标
- typescript - 获取 typescript 以推断元组参数类型
- html - İnstagram 视频网址无法播放 HTML5 播放器
- python - 如何将数据类型从对象转换为数字,然后在 pandas 中找到每一行的平均值?例如。将 '<17,500, >=15,000' 转换为 16250(平均值)
- html - 使用 HTML 和 CSS 正确显示等距图块
- command-line - 通过 CLI 发送多个参数时出现赛普拉斯错误
- javascript - 如何在反应表中插入按钮?