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
Add a ment  | 

3 Answers 3

Reset to default 2

Instead 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