$("form").submit not working on ipad and iphone

I have a simple post form working in all browsers but when it gets to ipad/iphone (I am new in iOS this world by the way) I got a problem.

<form action="/Home/Contact" id="contactform" method="post" novalidate="novalidate">                
<div class="response"></div>
    <p>
        <input class="textfield" data-val="true" data-val-length="The Name must be 50 characters or less" data-val-length-max="50" data-val-regex="The Name should not contain special characters" data-val-regex-pattern="^[A-Za-z0-9-_ ]+$" data-val-required="The Name field is required." id="Name" name="Name" placeholder="Name" type="text" value="">
        </p><div class="error-holder"><span class="field-validation-valid" data-valmsg-for="Name" data-valmsg-replace="true"></span></div>
    <p></p>
    <p>
        <input class="textfield" data-val="true" data-val-email="The Email field is not a valid e-mail address." data-val-length="The Email must be 200 characters or less" data-val-length-max="200" data-val-required="The Email field is required." id="Email" name="Email" placeholder="Email" type="text" value="">
        </p><div class="error-holder"><span class="field-validation-valid" data-valmsg-for="Email" data-valmsg-replace="true"></span></div>
    <p></p>
    <p>
        <input class="textfield" data-val="true" data-val-length="The Subject must be 255 characters or less" data-val-length-max="255" data-val-required="The Subject field is required." id="Subject" name="Subject" placeholder="Subject" type="text" value="">
        </p><div class="error-holder"><span class="field-validation-valid" data-valmsg-for="Subject" data-valmsg-replace="true"></span></div>
    <p></p>
    <p>
        <textarea class="textarea" cols="20" data-val="true" data-val-length="The Message must be 2000 characters or less" data-val-length-max="2000" data-val-required="The Message field is required." id="Message" name="Message" placeholder="Message" rows="8"></textarea>
        </p><div class="error-holder"><span class="field-validation-valid" data-valmsg-for="Message" data-valmsg-replace="true"></span></div>
    <p></p>
    <p>
        <button type="submit" name="submit" id="submitButton" title="Click here to submit your message!" class="btn btn-red">Submit</button>
    </p>
</form>

iPad and iPhone safari browsers just ignored jquery $("form").submit and form getting posted directly to controller.

Here is my submit code:

$("form").submit(function(e){

        if (!$("form").valid()) 
            return false;   

        var name = $('input#Name').val();
        var email = $('input#Email').val();
        var subject = $('input#Subject').val();
        var message = $('textarea#Message').val();
        $.ajax({
            type: 'post',
            url: '/home/contact',
            data: JSON.stringify({ name: name, email: email, subject: subject, message: message }),
            contentType: "application/json; charset=utf-8", 
            dataType: "json",
            cache: false,
            success: function(result) {
                //do stuff
            },
            error: function(result) {                                   
                //do stuff
            }               
        })

        return false;
    });

The code just not executed at all when I am clicking submit.

UPDATE1:

if I change to $("#contactform").submit(...) or $("#submitButton").click(...) still no luck on iphone and ipad

UPDATE2

If I use action="javascript:chk()" like @Sridhar-R advised it is working. But why javascript post not working when action="/Home/Contact" - is it all because in iphone/ipad javascript post working only that way?


Try with this

<form action="javascript:chk()" id="contactform" method="post" novalidate="novalidate">                
<div class="response"></div>
    <p>
        <input class="textfield" data-val="true" data-val-length="The Name must be 50 characters or less" data-val-length-max="50" data-val-regex="The Name should not contain special characters" data-val-regex-pattern="^[A-Za-z0-9-_ ]+$" data-val-required="The Name field is required." id="Name" name="Name" placeholder="Name" type="text" value="">
        </p><div class="error-holder"><span class="field-validation-valid" data-valmsg-for="Name" data-valmsg-replace="true"></span></div>
    <p></p>
    <p>
        <input class="textfield" data-val="true" data-val-email="The Email field is not a valid e-mail address." data-val-length="The Email must be 200 characters or less" data-val-length-max="200" data-val-required="The Email field is required." id="Email" name="Email" placeholder="Email" type="text" value="">
        </p><div class="error-holder"><span class="field-validation-valid" data-valmsg-for="Email" data-valmsg-replace="true"></span></div>
    <p></p>
    <p>
        <input class="textfield" data-val="true" data-val-length="The Subject must be 255 characters or less" data-val-length-max="255" data-val-required="The Subject field is required." id="Subject" name="Subject" placeholder="Subject" type="text" value="">
        </p><div class="error-holder"><span class="field-validation-valid" data-valmsg-for="Subject" data-valmsg-replace="true"></span></div>
    <p></p>
    <p>
        <textarea class="textarea" cols="20" data-val="true" data-val-length="The Message must be 2000 characters or less" data-val-length-max="2000" data-val-required="The Message field is required." id="Message" name="Message" placeholder="Message" rows="8"></textarea>
        </p><div class="error-holder"><span class="field-validation-valid" data-valmsg-for="Message" data-valmsg-replace="true"></span></div>
    <p></p>
    <p>
        <button type="submit" name="submit" id="submitButton" title="Click here to submit your message!" class="btn btn-red">Submit</button>
    </p>
</form>

Script

function chk(e){

    if (!$("form").valid()) 
        return false;   

    var name = $('input#Name').val();
    var email = $('input#Email').val();
    var subject = $('input#Subject').val();
    var message = $('textarea#Message').val();
    $.ajax({
        type: 'post',
        url: '/home/contact',
        data: JSON.stringify({ name: name, email: email, subject: subject, message: message }),
        contentType: "application/json; charset=utf-8", 
        dataType: "json",
        cache: false,
        success: function(result) {
            //do stuff
        },
        error: function(result) {                                   
            //do stuff
        }               
    })

    return false;
}
链接地址: http://www.djcxy.com/p/55726.html

上一篇: 如何防止在使用移动Safari浏览器时从缓存中重新加载网页?

下一篇: $(“form”)。提交不工作在iPad和iPhone上