表单验证返回false,但ajax不会停止表单提交

时间:2016-06-21 02:29:17

标签: jquery ajax forms validation ajaxform

我有一个多部分表单,首先是邮政编码检查,以验证申请人是否在我的服务区域内。当提交邮政编码时,它会运行检查并正确执行zip验证,据我所知,但无论是否验证为真或假,它都会转到表单的下一部分,前提是它已通过<div class="nb-main"> <ul class="list-navbar"> <li ui-sref="base.home"> <ng-include src="'svg/navbar-home.svg'"></ng-include> </li> <li ui-sref="base.home"> <ng-include src="'svg/navbar-interaction.svg'"></ng-include> </li> <li ui-sref="base.home"> <ng-include src="'svg/navbar-discover.svg'"></ng-include> </li> </ul> <div class="nb-img line-navbar"></div> </div> 检查。

add_zip.length > 4

此外,<SCRIPT type="text/javascript"> $(document).ready(function() { $("#add_zip").change(function() { var add_zip = $("#add_zip").val(); var msgbox = $("#result-msg"); if(add_zip.length > 4) { $("#result-msg").html('Checking availability...'); $.ajax({ type: "POST", url: "zipcheck.php", data: {add_zip : add_zip}, beforeSend: function() { $("#product_id").html('<option> Loading ...</option>'); }, success: function(msg){ if(msg == 'OK') { $("#add_zip").removeClass("invalid"); $("#enroll_county").prop('disabled', false); msgbox.html('You are in our service area.'); } else { $("#add_zip").addClass("invalid"); $("#enroll_county").prop('disabled', true); msgbox.html(msg); console.log("after msg !== 'OK' finishes"); } console.log("after success: function(msg) finishes"); } }); console.log("after ajax function"); } else { $("#add_zip").addClass("red"); $("#enroll_county").prop('disabled', true); $("#result-msg").html('<span id="status">Please Enter atleast 5 digits</span>'); } return false; }); }); </SCRIPT> <form action="" method="POST" class="form-validate" > <h2>Online Enrollment <span class="step_dcl">(Step 1 of 6)</span></h2> <p class="form_area"> <label id="add_zip-lbl" class="hasTip required" title="" for="add_zip"><span class="star_req">*</span>Please enter your zip code</label> <input type="text" name="add_zip" id="add_zip" value="<?=$this->enroll_form['add_zip'] ?>" class="inputbox required"> <span id="result-msg"></span> </p> <input type="submit" name="enroll_county" id="enroll_county" class="enroll_btn" value="ENROLL ONLINE"> </form> 会在下一个表单部件加载到我的服务区域之前显示在页面上,并且如果我单击文本框或单击它,它将完美运行选项卡而不是在输入zip后点击输入。

非常感谢任何帮助:)

更新:console.log显示:

msgbox.html(msg);

我相信这意味着表单仍在提交,因为ajax请求以某种方式丢失了时间?但我不知道如何解决这个问题。有什么想法吗?

0 个答案:

没有答案