jquery ajax jqXHR.status 始终为 0

标签 jquery

我正在尝试使用 jquery 通过 AJAX 提交表单:

<form name="myform" action="">
<table id="webcam-table">
    <thead>
        <tr>
            <th>Name</th>
            <th>...</th>
            <th><input type="checkbox" name="checkboxselectall" title="Select All" />&nbsp;&nbsp;
                <button type="submit" class="deletebutton" name="delete_video" title="Delete the selected videos">Delete</button></th>
        </tr>
    </thead>
    <tbody>
        <tr >
            <td>some data</td>
            ...
            <td><input type="checkbox" value="<?php echo $this->result_videos[$i]["video_name"]; ?>" title="Mark this video for deletion"/></td>
        </tr>
    </tbody>
</table>
</form>

如果有人选择一个复选框并点击提交按钮,我会得到以下代码:

jQuery(".deletebutton").on("click", function() {
    var testchecked = jQuery(':checkbox:checked').length;
    if (testchecked == 0) {
        alert('Please select at least one video');
        e.preventDefault();
    }
    else
    {
        if (confirm("Are you sure you want to delete the selected videos?"))
        {
            var checked = jQuery('input:checkbox:checked').map(function () {
                return this.value;
            }).get();
            var $this = jQuery(this);
            jQuery.ajax({
                type: 'POST',
                url: 'index.php?option=com_recordings&task=deletevideos&format=raw',
                data: {checkedarray:checked},
                success: function(data){
                    alert(data);

                }
            });
        }
    }
});

奇怪的是,这段代码在 IE 中运行良好(如何更改),但在 Chrome 或 FF 中却不起作用。如果我检查错误:

error: function(jqXHR, textStatus, errorThrown) {
        if (jqXHR.status === 0) {
            alert("Not connected. Verify Network.");
        }

它似乎总是抛出此警报。那么为什么它总是0呢?这是真正奇怪的事情。经过多次尝试后,如果我将表格更改为表格中的形式,它就可以工作:

<table id="webcam-table">
    <thead>
        <tr>
            <th>Name</th>
            <th>...</th>
            <th><input type="checkbox" name="checkboxselectall" title="Select All" />&nbsp;&nbsp;
                <button type="submit" class="deletebutton" name="delete_video" title="Delete the selected videos">Delete</button></th>
        </tr>
    </thead>
    <tbody>
<form name="myform" action="">
... //the rest is the same

这将在 FF 和 Chrome 中工作,但现在 IE 无法正确呈现表格。所以这不是一个解决方案(无论如何它不是 w3c 有效代码)。那么...有什么想法吗?我可以尝试调试什么吗?

最佳答案

这是因为你没有取消提交按钮的点击事件。您需要针对两种情况执行此操作,而不仅仅是 if 中的部分。将该行移到 if 之前,代码将神奇地不再为零。

关于jquery ajax jqXHR.status 始终为 0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14313283/

相关文章:

asp.net-mvc-3 - 如何将部分 View 结果附加到 div 中?

jquery - 如何编写仅在位置 :fixed is not supported or Jquery-test for position:fixed? 时适用的 CSS 规则

jquery - 在 Asp.Net Mvc 中使用 Ajax 刷新表列表

javascript - 如何将文本区域中粘贴的文本分成由相同字符数组成的 <p> 段落?

javascript - CKEDITOR 错误代码 : editor-element-conflict

javascript - jquery 使用绑定(bind)与点击

javascript - 为什么这些像素等效元素具有不同的实际大小?

javascript - 简单的 onclick 无法重定向 url

javascript - 使用 jquery 选择表中的 TR。

javascript - 如何突出显示 HTML5 表格中选定/单击的单元格