jquery ajax post 在 IE、FF、Chrome 中工作正常,但在 Safari 中不行
我通过操作方法将表单发布到远程服务器,但在发布之前调用 jquery 函数来更新数据库 - 在 IE、FF 等中一切正常......但是,Safari 总是无法执行 jquery 数据库当 return 为 true 或 async: true, ... 如果两者都为 false 时插入(因此 html 表单由于 false 声明而无法发布)它工作正常吗?但是当 true 时,表单只会 POST,但 jquery ajax post 将不会运行?
表单...
<form action="http://anotherplace" method="post" id="myform" >
<button class="button-big" type="submit" id="myform-post" ><span>Continue</span></button>
函数...
$(document).ready(function() { //available when doc renders
$("form#myform").submit(function(){
var title = $("select#title").val();
if (title == "") {
$("select#title").css({backgroundColor:"#ccc"});
$("select#title").focus();
return false;
}
var firstname = $("input#fname").val();
if (firstname == "") {
$("input#fname").css({backgroundColor:"#ccc"});
$("input#fname").focus();
return false; }
var surname = $("input#sname").val();
if (surname == "") {
$("input#sname").css({backgroundColor:"#ccc"});
$("input#sname").focus();
return false; }
//Grab the form values to pass via AJAX for DB insert
var cameraMake = $("input#cameraMake").val();
var cameraModel = $("input#cameraModel").val();
var cameraValue = $("input#cameraValue").val();
var mp3Make = $("input#mp3Make").val();
var mp3Model = $("input#mp3Model").val();
var mp3Value = $("input#mp3Value").val();
var dataString='&title='+ title + '&firstname=' + firstname + '&surname=' + surname + '&add1=' + add1 + '&add2=' + add2 + '&add3=' + add3 + '&pcode=' + pcode + '&email=' + email + '&phone=' + phone +'&username=' + username + '&pword=' + pword;
var ajaxComplete = false;
if( !ajaxComplete ){
$.ajax({
async: true,
type: "POST",
url: "process-.php",
data: dataString,
success: function() {
// $('#details').html("<div id='post-message'></div>");
// $('#post-message').html("<h2 style='color:#fff'>Thanks for getting in touch.</h2>")
ajaxComplete = true;
this.submit();
}
});
return false; // Abort form submission by now
}else{
return true; // Submit normally
}
});
}); //End of document.ready
脚本在基于提交 ID 的“点击”方法上运行(然后进行 AJAX 发布验证)。
有人可以就 Safari 中的失败(所有版本都经过测试)提供任何建议,或者提供更好的解决方案来完成我需要的操作吗? - 与异步/发布有关?正如我所说,设置为 false 并且工作正常...但是,我仍然需要 HTML 表单才能发布!
非常感谢!
I am posting a form to a remote server via the action method, but calling a jquery function to updata a database prior to the post - all works fine in IE, FF etc. ... but, Safari always fails to do the jquery database insert when return is true OR async: true, ... if both are false, (so the html form does not manage to post due to the false declaration) it works perfectly? But when true, the form will just POST, but the jquery ajax post will not run?
The Form ...
<form action="http://anotherplace" method="post" id="myform" >
<button class="button-big" type="submit" id="myform-post" ><span>Continue</span></button>
The function ...
$(document).ready(function() { //available when doc renders
$("form#myform").submit(function(){
var title = $("select#title").val();
if (title == "") {
$("select#title").css({backgroundColor:"#ccc"});
$("select#title").focus();
return false;
}
var firstname = $("input#fname").val();
if (firstname == "") {
$("input#fname").css({backgroundColor:"#ccc"});
$("input#fname").focus();
return false; }
var surname = $("input#sname").val();
if (surname == "") {
$("input#sname").css({backgroundColor:"#ccc"});
$("input#sname").focus();
return false; }
//Grab the form values to pass via AJAX for DB insert
var cameraMake = $("input#cameraMake").val();
var cameraModel = $("input#cameraModel").val();
var cameraValue = $("input#cameraValue").val();
var mp3Make = $("input#mp3Make").val();
var mp3Model = $("input#mp3Model").val();
var mp3Value = $("input#mp3Value").val();
var dataString='&title='+ title + '&firstname=' + firstname + '&surname=' + surname + '&add1=' + add1 + '&add2=' + add2 + '&add3=' + add3 + '&pcode=' + pcode + '&email=' + email + '&phone=' + phone +'&username=' + username + '&pword=' + pword;
var ajaxComplete = false;
if( !ajaxComplete ){
$.ajax({
async: true,
type: "POST",
url: "process-.php",
data: dataString,
success: function() {
// $('#details').html("<div id='post-message'></div>");
// $('#post-message').html("<h2 style='color:#fff'>Thanks for getting in touch.</h2>")
ajaxComplete = true;
this.submit();
}
});
return false; // Abort form submission by now
}else{
return true; // Submit normally
}
});
}); //End of document.ready
The script runs on an 'click' method based on the submit ID (does validation then the AJAX post).
Can ayone offer any advice on whe this fails in Safari (all versions tested) or offer a better solution to do what I need? - something to do with async / posting? As I say, set to false and it works fine ... BUT, I need the HTML form to still post!
Many Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果我理解正确的话,您希望在有待处理的 AJAX 请求时卸载页面。即使在它看起来可以工作的浏览器中,它也几乎没有机会正常工作。
我的建议是首先中止表单的 POST 请求(
return false;
),并向success
处理程序添加代码,以便在 AJAX 请求完成时触发表单提交。您可以使用标志变量,以便 AJAX 请求仅在第一次触发。更新:
一些快速、肮脏且未经测试的代码。我还意识到你省略了代码块的开头,所以我简单地发明了它:
If I understand correctly, you want to unload the page while there's a pending AJAX request. That has little chance to work correctly, even in the browsers where it appears to work.
My advice is to initially abort the form's POST request (
return false;
) and add code to thesuccess
handler to trigger the form submission when the AJAX request completes. You can use a flag variable so the AJAX request only triggers the first time.Update:
Some quick, dirty and untested code. I've also realised you've omitted the start of the code block so I've simply invented it:
你应该使用
然后使用表单操作
所以
you should use
and then go with form action
So