出于某种原因,我的 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 脚本附加到文档中,并且在初始加载时不在页面上,因此这可能会产生影响;我不确定。
最佳答案
步骤:
- 将
onclick
属性更正为onclick="sendTheData(event);"
- 检查
url
是否正确。 - 将方法从
GET
更改为POST
。
有关表单类的注意事项:您可以将 id
设置为特定表单,然后将变量绑定(bind)到该 id
而不是 class
。尝试避免使用 parent()
和类似的函数,因为它们使用了太多的资源(您要循环遍历所有元素,然后遍历父元素,然后找到正确的元素,而不是简单地循环)直接瞄准正确的目标)。
关于javascript - AJAX 在 Safari 或 Safari 移动设备中未触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26328835/