开发者

Problem with a submit input and jQuery validation

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;

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜