jQuery AJAX适用于IE但不适用于Firefox

时间:2014-09-29 18:22:11

标签: javascript jquery ajax

如果有人能告诉我我需要做些什么才能让这些代码在IE和Firefox中运行,我将不胜感激。在Firefox中,确认提示工作,但不会发生AJAX请求。 Firebug报告此错误:jquery-1.11.1.js(第9631行)。这是在该行找到的jquery代码:

xhr.send( ( options.hasContent && options.data ) || null );

jQuery代码:

$(document).ready(function() {
    $("#frmDelFiles").submit(function(event) {
        var names      = "";
        var id_array   = [];
        var elements = document.getElementsByName('files2del');

       for (var i = 0, l = elements.length; i < l; i++)
       {
          if (elements[i].checked)
          {
            //alert("ip_attachments record " + elements[i].id + " - " + elements[i].value + " is checked.");
            var response = confirm("Do you consent to delete the " + elements[i].value + " file?\n\nClick OK if Yes, otherwise Cancel.");
            if (response == true) {
                id_array[id_array.length] = elements[i].id;
                if (names == "") {
                    names = elements[i].value;
                } else {
                       names += ", " + elements[i].value;
                    }
            }
        }
        }

        if (names == "") {
            alert("Zero files have been selected for deletion.");
        } else {
            $.ajax({
                type: "POST",
                url: "del_ipa_rcd.php",
                data: {id_array: id_array, names: names},
                dataType: "html",
                success: function(response){
                   $("#delete_result").html(response);
                   //alert(response);
                }
            });
       }
    });
});

PHP:

<form name='frmDelFiles' id='frmDelFiles' method='POST' class='frmFileUpload'>
<input type='submit' value='Delete Checked Files' name='delFiles' class='btnSubmit' />
</form>

...
...

while ($attach_row = mysql_fetch_array($result, MYSQL_ASSOC)) {
   echo "            <input type='checkbox' name='files2del' id='" . $attach_row['id'] . "' value='" . $attach_row['name'] . "'>\n";
   echo "            <a target='_blank' href='/php/dwnld_blob.php?column=content&tbl=ip_attachments&id=" . $attach_row['id'] . "'>" . $attach_row['name'] . "</a><br>\n";
}

1 个答案:

答案 0 :(得分:0)

我能够通过改变html中的几行和jQuery中的一行来解决这个AJAX请求问题。

以下是html更改:

<form name='frmDelFiles' id='frmDelFiles' method='POST' class='frmFileUpload'>
    <input type='submit' value='Delete Checked Files' name='delFiles' class='btnSubmit' />
</form>

更改为:

<button class="btnSubmit">Delete Checked Documents</button>

这是一个jQuery更改

$("#frmDelFiles").submit(function(event) {

更改为:

$("button").click(function() {

现在,AJAX请求在IE和Firefox中都能正常运行。