admin管理员组文章数量:1406471
I need a way to submit the form ONLY if foo return true. I tried this code but it submits the form even if foo return false.
<input type="submit" value="submit" onclick="return foo();" />
<script type="text/javascript">
function foo() {
// if... return true
// else return false
}
</script>
If I use the onsubmit function, then it submits first. So it's not good in my case.
I need a way to submit the form ONLY if foo return true. I tried this code but it submits the form even if foo return false.
<input type="submit" value="submit" onclick="return foo();" />
<script type="text/javascript">
function foo() {
// if... return true
// else return false
}
</script>
If I use the onsubmit function, then it submits first. So it's not good in my case.
Share Improve this question edited Jul 27, 2015 at 2:03 sfletche 49.9k31 gold badges109 silver badges120 bronze badges asked Jul 25, 2015 at 5:23 xRobotxRobot 26.6k72 gold badges192 silver badges317 bronze badges 10- is there any error in your browser console – Arun P Johny Commented Jul 25, 2015 at 5:24
- no, any error in the console – xRobot Commented Jul 25, 2015 at 5:26
- 1 jsfiddle/arunpjohny/e4tdzme4/2 - looks fine - can you try to recreate the issue in the fiddle – Arun P Johny Commented Jul 25, 2015 at 5:27
- HTML5 form validation will do this automatially...? – xyz Commented Jul 25, 2015 at 5:28
- I know, but it must work on old browsers too. – xRobot Commented Jul 25, 2015 at 5:29
3 Answers
Reset to default 2you can try alternative method using jquery ajax submit as follows, which I am using, may helpful to u too..
var request;
$("#YourFormId").submit(function(event){
if(yourFunction()==false) {
return false;
}
if (request) {
request.abort();
}
var $form = $(this);
var $inputs = $form.find("input, select, button, textarea");
var serializedData = $form.serialize();
$inputs.prop("disabled", true);
request = $.ajax({
url: "yourFormActionPage.php",
type: "post",
data: serializedData
});
request.done(function (response, textStatus, jqXHR){
//ok
});
request.fail(function (jqXHR, textStatus, errorThrown){
//track it
});
request.always(function () {
$inputs.prop("disabled", false);
});
event.preventDefault();
});
One possible solution is to use the form's submit event. You can put the conditional in the submit event handler so the submission is halted when foo()
does not return true.
(NOTE: I had to guess what the selector might be for your form since it's not part of your question)
document.getElementById('myForm').submit(function(e) {
if (!foo()) {
e.preventDefault();
}
})
You can use ajax submit.
<form class="formbox" method="post" action="/your/action/url" id="myForm" name="myForm">
<input type="text" id="name" class="name" name="name" value=""/>
<input type="submit" value="submit" onclick="return foo();" />
</form>
<script src="http://ajax.googleapis./ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script type="text/javascript">
function foo() {
if(1==1) {
alert("Success: Going to call the function");
$("#myForm").submit(); //calling function.
} else {
alert("Error: ");
}
}
本文标签: javascriptSubmit form only if a function return trueStack Overflow
版权声明:本文标题:javascript - Submit form only if a function return true - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745014694a2637764.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论