AJAX不在Safari或Safari移动中触发

时间:2014-10-12 19:02:22

标签: javascript php jquery ajax safari

出于某种原因,我的Mac或我的iPad / iPhone上的Safari中没有触发我的AJAX事件。我是新手,所以可能(也可能是)程序员'错误,但这是我的代码:

<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脚本附加到文档中,并且在初始加载时不在页面上,因此可能会影响事物;我不确定。

2 个答案:

答案 0 :(得分:0)

尝试添加失败回调,也许你会收到一些错误...... 你可以在http://api.jquery.com/jQuery.ajax/

上找到一个例子

答案 1 :(得分:0)

步骤:

  • onclick属性更正为onclick="sendTheData(event);"
  • 检查url是否正确。
  • 将方法从GET更改为POST

关于表单类的注意事项:您可以将id设置为特定表单,然后将变量绑定到id而不是class。尝试避免parent()和类似的功能,因为他们使用了太多的资源(你通过所有元素循环,然后通过父母,然后找到正确的,而不是简单地瞄准正确的直接地)。

相关问题