$(“form”)。提交不工作在iPad和iPhone上
我有一个简单的发布表单,可以在所有浏览器中工作,但当它到达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和iPhone safari浏览器忽略了jquery $(“form”)。submit和form直接发布到控制器。
这是我的提交代码:
$("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;
});
当我点击提交时,代码完全没有执行。
UPDATE1:
如果我更改为$("#contactform").submit(...)
或$("#submitButton").click(...)
iPhone和iPad上仍然没有运气
UPDATE2
如果我使用action="javascript:chk()"
就像@ Sridhar-R建议它正在工作。 但是,为什么JavaScript发布不工作时的action="/Home/Contact"
- 这是全部因为在iphone/ipad
JavaScript发布工作只有这样?
试试这个
<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>
脚本
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/55725.html