I have a form like that:
<script type="text/javascript">
function checkpas()
{
var newp = $('#NewPassword').val();
var newpp = $('#RepeatedNewPasswo开发者_如何学Pythonrd').val();
if (newp != newpp)
{
return false;
}
}
</script>
<% using (Html.BeginForm()) {%>
<%: Html.ValidationSummary(true) %>
...
<input type="submit" id="sub_btn" value="Edit" onclick="checkpas();" />
<% } %>
but that submit input always invokes a form submit, even if I return false in the JS code. Why ?
you need to run onsubmit() not onclick()
Like this:
<script type="text/javascript">
$("#yourformID").submit(function (evt)
{
var newp = $('#NewPassword').val();
var newpp = $('#RepeatedNewPassword').val();
if (newp != newpp)
{
return false;
}
}
</script>
Note assign a form ID by doing this in your ASPX. here is a reference http://msdn.microsoft.com/en-us/library/dd492714.aspx
<% using (Html.BeginForm("ActionName", "ControllerName", Method, new {id="myFormID")})) {%>
I believe you need to return the function result: instead of:
<input type="submit" id="sub_btn" value="Edit" onclick="checkpas();" />
use:
<input type="submit" id="sub_btn" value="Edit" onclick="return checkpas();" />
also in your function add an else return true;
精彩评论