我有一个带有“必填”字段的表单,所以Chrome会在提交表单时检查是否所有字段都被按下了。
但是,我想向按钮添加一些JS,但在本例中,我的操作覆盖了默认的浏览器行为,以检查所需的字段。
有没有一种方法可以在浏览器验证所有字段都已填写后,在单击按钮时执行Javascript,而无需手动执行该检查?
发布于 2016-05-10 14:33:53
好的
如下所示:
HTML
<form onsubmit="return checkform()">
JS
function checkform()
{
//Code here
return true; //return false for cancel the submit
}
示例
function checkform()
{
if($("#name").val()!="John")
{
alert("checkform alert");
return false;
}
alert("Ok");
return true;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form name="monform" onsubmit="return checkform()">
<input type="text" id="name" required>
<button type="submit">Send</button>
</form>
https://stackoverflow.com/questions/37141486
复制