Ajax表单不工作时返回false
Return false on Ajax form not working
我在中有一个表单,用于评估流程页面上数据库中的信息,并在必要时返回错误。我使用Ajax,所以它实际上不应该进入流程页面并加载我在Json中编码的内容来返回。这是我的表单加上Javascript:
<form method="post" action="../user/process_login" id="login_form">
<input type="hidden" name="action" value="login">
<label>Email Address:</label>
<input type="text" id="email" name="email" placeholder="Email" />
<label>Password:</label>
<input type="password" id="password1" name="password0" placeholder="Password" />
<input type="submit" id="submitbtn" placeholder="Submit" class="button"/>
</form>
Javascript:
<script type="text/javascript">
$(document).ready(function(){
$('#login_form').submit(function(){
$.post
(
$(this).attr('action'),
$(this).serialize(),
function(data){
if (data['errors'] == '') {
consle.log(data);
};
else{
console.log(data);
$('#alert_box').html(data);
};
},
"json"
);
return false;
});
});
</script>
这是我的验证代码的相关部分:
if (count($user) > 0 AND $decrypted_password == $this->input->post('password0'))
{
$this->session->set_userdata('user_session', $user);
$this->load->view('main.php');
}
else
{
$errors = "<div class='alert-box alert' id='error-box'><p>Your login information did not match our reccords. Try again</p></div>";
echo json_encode($errors);
}
我认为这是因为您的代码中存在错误。我在下面指出
这里没有错误---->没有错误
这是错误----->错误
正如您所看到的,返回false的操作不会出错。
<script type="text/javascript">
$(document).ready(function(){
$('#login_form').submit(function(){
$.post
(
$(this).attr('action'),
$(this).serialize(),
function(data){
if (data['errors'] == '') {
consle.log(data);
}; <----------------------- Error Not supposed to be there
else{
console.log(data);
$('#alert_box').html(data);
}; <----------------------- I think will still work but don't need
},
"json"
);
return false;
});
});
</script>
在调用Ajax函数时,不能使用输入类型submit,如果使用它,代码将无法按预期工作,因此将其更改为:
<input type="button" id="submitbtn" placeholder="Submit" class="button"/>
相关文章:
- Array.every返回false,而不是类型error
- Javascript If else 只返回 TRUE 或只返回 FALSE
- 为什么当我在数组上测试regex时,它会返回false
- 检查文本区域是否为空总是返回false
- PHP strtotime()为JavaScript日期字符串返回false
- onsubmit返回false,但submit仍然执行
- 这个代码会返回false吗
- Ajax向表单返回false
- 表单验证JavaScript编号总是返回false
- 使用!而in运算符在应该为true时返回false,为什么
- jQuery.is(':checked')总是返回false
- Javascript 下划线每个总是返回 false
- 提交返回 false 总是在 jquery 中
- 为什么 1.2 == true 返回 false 如果布尔值 (1.2) 实际上是真的
- 如果循环中至少有一个元素返回 false,如何将变量设置为 false
- 基本JavaScript不断返回false
- 布尔对象上的逻辑 NOT 在 Javascript 中总是返回 false
- jquery focusin没有'调用.focus()并返回false;时无效;
- Ajax-执行成功前返回false
- jQuery JSON flickr提要返回false不工作