$("#qwerq").submit(function (e){
e.preventDefault();
var check=0;
if($("#firstName").val() == "") {
check=1;
}
if(check!=1){
$("#qwerq").unbind("submit") ;
$("#qwerq").submit();
//$("#qwerq").trigger('submit', [true]);
}
});
当表单具有id="qwerq"
时,按需且提交未绑定,表单不会自行提交。
我尝试使用.submit()
和.trigger("submit")
。我必须再次手动单击“提交”。
我应该添加什么以便不必再次单击?
而不是取消绑定事件,为什么不只阻止仅在错误时提交?
if(check === 1) return false;
jQuery的事件处理程序中的[return false
表示preventDefault
和stopPropagation
。
我认为您正在尝试通过单击按钮提交表单。然后,您需要在代码中进行一些更改:
为表单提供ID并更改按钮type =“ button”(而不是“ submit”):
<form id="form_1" action="yourserverpagename.php" method="post">
<input id="firstName" type="text" value="" />
<input id="qwerq" type="button" value="Send" />
</form>
现在您的脚本应如下所示:
<script>
$("#qwerq").click(function (e) {
e.preventDefault();
var check = 0;
if ($("#firstName").val() == "") {
check = 1;
}
if (check != 1) {
//-- here is the way to submit the form using form id --
$('#form_1').submit();
}
});
</script>