我遇到Ajax submitHandler的问题。问题是这样的,如果值不存在那么它就不会允许用户提交表单并在文本框下面显示和提醒但如果数据存在则表单将提交。我有数据存在的问题但是当我用firebug进行调试时,submithandler不会在表单中只返回值。
以下是表格代码:
<form action="<?php echo base_url(); ?>home/reward" id='myFormId' method="post">
<div class="header-content black wow fadeInLeft animated animated" data-wow-delay="0.6s">
<div class="header-content-inner" style="text-align: center !important;">
<div style="margin:auto;">
<p>
<input type="text" id="cardcodeno" name="cardcodeno" required maxlength="14" >
</p>
<label id="errorMesg"></label>
</div>
<div style="margin:auto;">
<p>
<input type="text" id="noponsel" name="noponsel" required maxlength="14">
</p>
</div>
</div>
</div>
<div class="header-button">
<input type="submit" name="submit" id="btnSubmit" value=" ">
</div>
</form>
这是ajax:
$(document).ready(function() {
var flag = false;
$("#myFormId").validate({
rules:{
noponsel:{required:true,number: true,minlength:10},
cardcodeno:{required:true},
metodaC:{email:true},
},
/*Check Data No Exist or Not*/
submitHandler: function (form) {
var cardcodeno = $('#cardcodeno').val();
var isSuccess = false;
if(!flag) {
$.ajax({
type: "POST",
dataType: "json",
url: base_url+"home/checkpassioncard",
data: {"cardcodeno":cardcodeno}
}).done(function (response) {
if(response == 0) {
$('#errorMesg').html('Data Not Exist, Please Type Correctly ');
}else {
flag = true;
$('#errorMesg').html('');
$( "#btnSubmit" ).trigger( "click" );
}
});
}
return flag;
},
messages:{
noponsel:{
required:'Phone no required',
number :'Only numeric',
minlength:'min 10 char'},
nopassioncard:{
required:'Passion card must be fill'},
metodaC: {
email :'Email must valid'},
},
errorElement: "p",
wrapper: "p", // a wrapper around the error message
});
});
控制器功能检查数据是否存在:
public function checkpassioncard()
{
$data = $this->input->post('cardcodeno');
$result = $this->Home_m->checkpassioncard($data);
echo $result;
}
这就是模特们:
public function checkpassioncard($data)
{
$sql = "SELECT * FROM mytable WHERE cardcodeno LIKE '$data' LIMIT 1";
$query = $this->db->query($sql);
if ($query->num_rows() == 0) {
return "0";
} else {
return "1";
}
}
===更新====
当我用firebug调试时,它总是返回这个代码更新
...
}else {
flag = true;
$('#errorMesg').html('');
$( "#btnSubmit" ).click( function(){
$.ajax({
url: base_url+"home/reward",
type: "POST",
data: {"noponsel":noponsel},
dataType: "json",
success: function(result){
alert("test");
$("#myFormId").submit();
}
});
});
}
...