[英]how to check for a condition before programmatically submitting a form in javascript
I have a form which I want to submit only if a condition is true. 我有一个表格,我想在条件成立时才提交。 But there is also a case in which I want to programmatically.
但也有一种情况,我想以编程方式。
Following works when manually submitting the form: 以下是手动提交表单时的工作原理:
<input type="submit" value="submit" onclick="return checkForTheCondition();"/>
Following works for the submitting the form programmatically but it doesn't check for the condition for submitting the form. 以下工作以编程方式提交表单,但不检查提交表单的条件。
$('form').submit();
So I tried to do the following for programmatically checking the condition before submitting the form but when I use the following code it doesn't even submit the form which was being done by $('form').submit();
所以我尝试在提交表单之前以编程方式检查条件,但是当我使用下面的代码时,它甚至没有提交由
$('form').submit();
完成$('form').submit();
: :
$('form').submit(function() {
return checkForTheCondition();
});
checkForTheCondition()
returns either true
or false
. checkForTheCondition()
返回true
或false
。
Is it possible to programmatically submit the form just like it works for manual form submission where it checks for a condition before submitting the form? 是否可以以编程方式提交表单,就像它适用于手动表单提交一样,它在提交表单之前检查条件?
You should be able to do it this way: 你应该能够这样做:
if (checkForTheCondition()) {
$('form').submit();
}
Edit: I think I understand your problem now. 编辑:我想我现在明白你的问题。
Calling $('form').submit(function () { ... });
调用
$('form').submit(function () { ... });
does NOT trigger the event. 不会触发事件。 The function is not a callback!
该功能不是回调! Instead it binds the function to the submit event.
相反,它将函数绑定到submit事件。 So when you call this:
所以当你这样称呼时:
$('form').submit(function() {
return checkForTheCondition();
});
You do not trigger the event, insted you bind return checkForTheCondition();
你没有触发事件,你绑定
return checkForTheCondition();
this is somewhat the same as onclick="return checkForTheCondition();"
这与
onclick="return checkForTheCondition();"
有些相同 but it will be called everytime the form is submitted, and not only when the submit button is clicked. 但每次提交表单时都会调用它,而不仅仅是在单击提交按钮时。
I would do it this way: 我会这样做:
<input type="submit" value="submit">
<script>
$(document).ready(function () {
$('form').submit(function() {
return checkForTheCondition();
});
});
</script>
Then you can just call $('form').submit();
然后你可以调用
$('form').submit();
and checkForTheCondition()
will be called each time. 并且每次都会调用
checkForTheCondition()
。 You can read more about jQuery and submit here . 您可以阅读有关jQuery的更多信息并在此处提交。 Good luck
祝好运
Edit again: 再次编辑:
I realise the easiest solution would be this: 我意识到最简单的解决方案是:
<form method="post" action="somepage" onsubmit="return checkForTheCondition();">
<!-- Inputs -->
<input type="submit" value="submit">
</form>
This will make $('form').submit();
这将使
$('form').submit();
and clicks on the submit button call checkForTheCondition()
before submitting. 并在提交之前点击提交按钮调用
checkForTheCondition()
。 Enjoy! 请享用!
Your code should work if it returns true/false conditionally. 如果有条件地返回true / false,您的代码应该有效。 You can try this if you want to alert any message on failure of condition.
如果要在条件失败时提醒任何消息,可以尝试此操作。
$(function(){
$('form').submit(function() {
if(!checkForTheCondition()){
//alert message here
return false;//This will stop the form from being submitted.
}
return true;
});
});
if ($( "#formID" ).submit()){
alert( "Handler for .submit() called." );
};
$('#my_form_id').submit(function() {
var my_value = checkForTheCondition();
if(my_value == "some returned value"){
return true;
} else {
alert("error in your submission");
return false;
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.