admin管理员组文章数量:1322838
In my web application, in the login page, I have a checkbox. After entering the username/password, if the user doesn't tick the checkbox (T&C), he should not be taken to the home page, and an alert error message will be shown.
I have tried the following code. But it is not working. That is, an alert message is shown. But the user is taken to the next page(home page). I tried returning false, but no luck.
Can anyone tell how to fix this?
function doSubmit() {
var checkbox = document.getElementById("terms");
if (!checkbox.checked) {
alert("error message here!");
return;
}
document.getElementById("f").submit();
}
I am calling doSubmit from
<input id="proceed" onclick="doSubmit()" type="submit" value="${fn:escapeXml(submit_label)}" />
In my web application, in the login page, I have a checkbox. After entering the username/password, if the user doesn't tick the checkbox (T&C), he should not be taken to the home page, and an alert error message will be shown.
I have tried the following code. But it is not working. That is, an alert message is shown. But the user is taken to the next page(home page). I tried returning false, but no luck.
Can anyone tell how to fix this?
function doSubmit() {
var checkbox = document.getElementById("terms");
if (!checkbox.checked) {
alert("error message here!");
return;
}
document.getElementById("f").submit();
}
I am calling doSubmit from
<input id="proceed" onclick="doSubmit()" type="submit" value="${fn:escapeXml(submit_label)}" />
Share
Improve this question
edited Jul 26, 2012 at 7:24
user1514499
asked Jul 26, 2012 at 6:56
user1514499user1514499
7717 gold badges26 silver badges64 bronze badges
2
- 2 how are you calling doSubmit() – bugwheels94 Commented Jul 26, 2012 at 7:01
-
@user1351052 Is
doSubmit
called in the submit event, a button click event, some other arbitrary call? – Musa Commented Jul 26, 2012 at 7:23
3 Answers
Reset to default 2Instead of using onclick
in input, try using the below in the form tag:
onsubmit="return doSubmit()"
And the js functions as:
function doSubmit() {
var checkbox = document.getElementById("terms");
if (!checkbox.checked) {
alert("error message here!");
return false;
}
}
Try changing the input type to button instead of submit, delegating the submit action to JS function:
<input id="proceed" onclick="doSubmit()" type="button" value="${fn:escapeXml(submit_label)}" />
Before you submit your form, you should check the checkbox
's status, if it's not checked, prevent the from from submit. In jQuery, the following code does the tricks.
$(form).submit(function(e){
if($(this).find("#terms").is('checked')){
e.preventDefault();
alert('error messge goes here');
}
}
本文标签: springJavascriptstop proceeding after alert messageStack Overflow
版权声明:本文标题:spring - Javascript - stop proceeding after alert message - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742117248a2421525.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论