在我的web应用程序中,在登录页面中,我有一个复选框。输入用户名/密码后,如果用户没有勾选复选框(t&C),则不应将其带到主页,并显示警告错误消息。
我已经尝试了以下代码。但它不起作用。也就是说,会显示一条警告消息。但是用户被带到下一个页面(主页)。我试着返回false,但没有成功。
有人能告诉我怎么解决这个问题吗?
function doSubmit() {
var checkbox = document.getElementById("terms");
if (!checkbox.checked) {
alert("error message here!");
return;
}
document.getElementById("f").submit();
}
我从打电话给doSubmit
<input id="proceed" onclick="doSubmit()" type="submit" value="${fn:escapeXml(submit_label)}" />
不要在输入中使用onclick
,而是尝试在表单标记中使用以下内容:
onsubmit="return doSubmit()"
js的作用是:
function doSubmit() {
var checkbox = document.getElementById("terms");
if (!checkbox.checked) {
alert("error message here!");
return false;
}
}
尝试将输入类型更改为按钮而不是提交,将提交操作委派给JS函数:
<input id="proceed" onclick="doSubmit()" type="button" value="${fn:escapeXml(submit_label)}" />
在提交表单之前,您应该检查checkbox
的状态,如果未检查,则阻止提交。在jQuery中,以下代码实现了这些技巧。
$(form).submit(function(e){
if($(this).find("#terms").is('checked')){
e.preventDefault();
alert('error messge goes here');
}
}