简体   繁体   English

$("form").submit 在 ipad 和 iphone 上不起作用

[英]$("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.我有一个可以在所有浏览器中使用的简单帖子表单,但是当它到达 ipad/iphone 时(顺便说一下,我是 iOS 这个世界的新手)我遇到了问题。

<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. iPad 和 iPhone Safari 浏览器只是忽略了 jquery $("form").submit 并将表单直接发布到控制器。

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:更新1:

if I change to $("#contactform").submit(...) or $("#submitButton").click(...) still no luck on iphone and ipad如果我更改为$("#contactform").submit(...)$("#submitButton").click(...)在 iphone 和 ipad 上仍然没有运气

UPDATE2更新2

If I use action="javascript:chk()" like @Sridhar-R advised it is working.如果我使用@Sridhar-R 之类的action="javascript:chk()"建议它正在工作。 But why javascript post not working when action="/Home/Contact" - is it all because in iphone/ipad javascript post working only that way?但是为什么 javascript post 在action="/Home/Contact"时不起作用 - 是不是因为在iphone/ipad javascript post 只能这样工作?

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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM