我有一个简单的帖子形式在所有浏览器中工作但是当它到达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”)。提交和表单直接发布到控制器。
这是我的提交代码:
$("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
如果我使用像@ Sridhar-R这样的action="javascript:chk()"
建议它正在运行。但是为什么javascript在action="/Home/Contact"
发布时无效 - 这是因为在iphone/ipad
javascript帖子只是那样工作?
答案 0 :(得分:0)
试试这个
<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;
}