出于某种原因,我的AJAX事件不会在我的Mac或iPad/iPhone上启动Safari。我对此很陌生,所以这可能是(也可能是)程序员的错误,但下面是我的代码:
<form class='send-form'>
<input type="hidden" name="csv-val" value="">
<input type="hidden" name="img-val" value="">
<button type='button' class='send-it' onclick='$(sendTheData(event))'>
Manual Submit
</button>
</form>
<script>
function sendTheData(event){
var sendButton = $(event.target);
var theForm = sendButton.parents('form');
var theFormClass = sendButton.parents('form').attr('class');
console.log("Parent form is " + theFormClass);
$.ajax({
async: "true",
type: 'GET',
url: 'save.php',
data: $(theForm).serialize()
}).done(function( msg ) {
alert( "Data Saved: " + msg );
});
return false;
}
</script>请注意,类send-form的元素是由不同的jQuery脚本附加到文档中的,并且在初始加载时不在页面上,因此这可能会影响事情;我不确定。
发布于 2014-10-12 19:39:54
步骤:
onclick属性更正为onclick="sendTheData(event);"url是否是正确的。GET更改为POST。关于表单的类,您可以将id设置为特定的表单,然后将变量绑定到该id,而不是class。尝试避免使用parent()和类似的函数,因为它们使用了太多的资源(您正在遍历所有元素,然后通过父元素,然后找到正确的元素,而不是直接针对正确的元素)。
发布于 2014-10-12 19:34:55
尝试添加失败回调,也许你会出错.你可以在http://api.jquery.com/jQuery.ajax/上得到一个例子
https://stackoverflow.com/questions/26328835
复制相似问题