我的表格是这样的:
<script type="text/javascript">
function checkpas()
{
var newp = $('#NewPassword').val();
var newpp = $('#RepeatedNewPassword').val();
if (newp != newpp)
{
return false;
}
}
</script>
<% using (Html.BeginForm()) {%>
<%: Html.ValidationSummary(true) %>
...
<input type="submit" id="sub_btn" value="Edit" onclick="checkpas();" />
<% } %>
但是提交输入总是调用表单提交,即使我在JS代码中返回false。为什么?
答案 0 :(得分:2)
你需要运行onsubmit()而不是onclick()
像这样:
<script type="text/javascript">
$("#yourformID").submit(function (evt)
{
var newp = $('#NewPassword').val();
var newpp = $('#RepeatedNewPassword').val();
if (newp != newpp)
{
return false;
}
}
</script>
注意在ASPX中执行此操作来指定表单ID。这是参考http://msdn.microsoft.com/en-us/library/dd492714.aspx
<% using (Html.BeginForm("ActionName", "ControllerName", Method, new {id="myFormID")})) {%>
答案 1 :(得分:1)
我相信你需要返回函数结果: 而不是:
<input type="submit" id="sub_btn" value="Edit" onclick="checkpas();" />
使用:
<input type="submit" id="sub_btn" value="Edit" onclick="return checkpas();" />
同样在您的函数中添加else return true;