简体   繁体   English

Javascript-收到警告消息后停止继续

[英]Javascript - stop proceeding after alert message

In my web application, in the login page, I have a checkbox. 在我的Web应用程序的登录页面中,我有一个复选框。 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. 输入用户名/密码后,如果用户未选中复选框(T&C),则不应将其带到主页,并且将显示警告错误消息。

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 我正在从中致电doSubmit

<input id="proceed" onclick="doSubmit()" type="submit" value="${fn:escapeXml(submit_label)}" />

Instead of using onclick in input, try using the below in the form tag: 而不是在输入中使用onclick ,请尝试在form标记中使用以下内容:

onsubmit="return doSubmit()"

And the js functions as: js的功能如下:

function doSubmit() {
    var checkbox = document.getElementById("terms");
    if (!checkbox.checked) {
        alert("error message here!");
        return false;
    } 
}

尝试将输入类型更改为button而不是Submit ,将Submit操作委托给JS函数:

<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. 在提交表单之前,您应检查checkbox的状态,如果未选中,则阻止提交。 In jQuery, the following code does the tricks. 在jQuery中,以下代码可以解决问题。

$(form).submit(function(e){
    if($(this).find("#terms").is('checked')){
        e.preventDefault();
        alert('error messge goes here');
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM