Press Enter to submit the form!

Today encountered in the project, press the Enter key Ajax went backstage to submit!

At first did not notice, there is no <input type = "submit"> how to achieve a carriage return submitted! Only one < A class = "userBut" ID = "userBut" the href = "JavaScript: void (0);" > mention & nbsp; & nbsp; deposit </ A >.

Check the check Baidu , I found the answer :

              1, if there is a form type = "submit" button, the Enter key to take effect
              2, if there is only one form type = "text" of the INPUT , no matter what button type, the Enter key to take effect .

<form>
   <div class="p_box lxUser">
            <div class="userBorder">
                <h2>输入邮箱</h2>
                <div class="userTable">
                    <lable for="Email"><span> 邮箱:</span></lable>
                    <input type="text" name="Email" id="Email" />
                    <a class="userBut" id="userBut" href="javascript:void(0);">&nbsp;&nbsp;</a>
                </div>
            </div>
   </div>
</form>

js code as follows:

<script type="text/javascript">
    $(function () {
        $(":input[type=text]").bind("blur", function () {
            var _this = $(this);
            if (_this.val().length == 0) {
                return false;
            }
            else {
                if (_this.val().match(/^\w+((-\w+)|(\.\w+))*\@@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) == null) { 
                    $ ( " .UserRed " .) The .Show () Find ( " #textfont " ) .html ( " Please enter the correct mailbox " );
                     return  to false ; 
                } 
                the else { 
                    $ ( " .userRed " ) .hide () ;
                     return  to true ; 
                } 
            } 
            return  to true ; 
        }) 

        // Submit form operation 
        var submitForm = function () {
             var data = $("form").serialize();
            $.ajax2({
                url: '@Url.Action("EnsureYourEmail", "FindPass")',
                data: data,
                type: "POST",
                dataType: "json",
                success: function (data) {
                    if (data.code) {
                        location.href = data.href;
                    }
                    else {
                        $(".userRed".) the .Show () Find ( " #textfont " ) .html (data.msg); 
                    } 
                }, error: function () { 
                    Alert ( " ! wrong " ); 
                } 
            }) 
        } 

        // return key submit the form 
        $ ( " form " ) .submit (function () { 
            submitForm (); 
            return  false ; 
        }) 


        // click the button to submit the form 
        $ ( " #userBut " ) .click (function () { 
            $ ( " : the INPUT [of the type = text] ").trigger("blur");
            if ($(".userRed:visible").length > 0 || $("#Email").val() == "" || $("#Email").val() == null) {
                return false;
            }
            submitForm();
        })
    })
</script>

 

 

 

Reproduced in: https: //www.cnblogs.com/Kummy/archive/2013/05/28/3103474.html

Guess you like

Origin blog.csdn.net/weixin_33964094/article/details/93230575