0

以下 javascript 函数似乎仅在我拥有最初用于调试目的的最终确认()语句时才起作用。当我把它拿出来时,delete_row.php 似乎没有运行。另外,也许作为提示/旁注,当我确实有确认语句时,它适用于除 safari 之外的所有浏览器...

function deleterow(form) {

    if (!confirm("Are you sure you want to delete?")) return false;

    var queryString = "?ID=";

    for (var i = 0; i < document.myForm.rows.length; i++) {
        if (document.myForm.rows[i].checked) {
            ID = document.myForm.rows[i].value;
            ID = ID.slice(0, -1);
            queryString += ID;
            queryString += "-";
        }
    }
    queryString = queryString.slice(0, -1);

    try{
        // Opera 8.0+, Firefox, Safari
        ajaxRequest = new XMLHttpRequest();
    } catch (e){
        // Internet Explorer Browsers
        try{
            ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            try{
                ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e){
                // Something went wrong
                alert("Your browser broke!");
                return false;
            }
        }
    }

    var ajaxRequest;  // The variable that makes Ajax possible!
    // Create a function that will receive data sent from the server
    ajaxRequest.onreadystatechange = function(){
        if(ajaxRequest.readyState == 4){
            var ajaxDisplay = document.getElementById('ajaxDiv');
            ajaxDisplay.innerHTML = ajaxRequest.responseText;
        }
    }

    ajaxRequest.open("GET", "delete_row.php" + queryString, true);
    ajaxRequest.send(null); 
    confirm('Delete successful!');
}

更新已解决

我正在通过以下 js 脚本更改检查 ajaxRequest 的状态

ajaxRequest.onreadystatechange = function(){ // Create a function that will receive data sent from the server
    if(ajaxRequest.readyState == 4 && ajaxRequest.status == 200){
        var ajaxDisplay = document.getElementById('ajaxDiv');
        ajaxDisplay.innerHTML = ajaxRequest.responseText;
    }
    else{
        alert('An error has occurred making the request');
        return false;
    }
}

并注意到我从服务器获得了 0 的状态。一些谷歌搜索帮助我意识到错误在于我如何定义调用这些函数的按钮。

原始代码是:

<div style='float:left; margin-right:10px;'><input type="submit" onClick="deleterow(document.myForm)" VALUE="Delete ROWs"></div>

修复是:

<div style='float:left; margin-right:10px;'><input type="button" onClick="deleterow(document.myForm)" VALUE="Delete ROWs"></div>

(提交类型必须更改为按钮类型)

4

3 回答 3

1

delete_row.php doesn't seem to run您是否验证过这一点,您是否可以向我尝试了您的 JS 虽然没有表单内容添加一个警报,if(ajaxRequest.readyState == 4){并且它似乎工作正常, http: //jsfiddle.net/6gjy6/您在 Google Chrome 控制台中是否收到任何 JS 错误?您是否尝试过使用适当的 url 在浏览器上执行基本的“GET”请求,delete_row.php" + queryString,并查看服务器如何响应而不是 AJAX 调用。

试试这个:

var queryString = "?ID=";

for (var i = 0; i < document.myForm.rows.length; i++) {
    if (document.myForm.rows[i].checked) {
        ID = document.myForm.rows[i].value;
        ID = ID.slice(0, -1);
        queryString += ID;
        queryString += "-";
    }
}
queryString = queryString.slice(0, -1);
var ajaxRequest; 

try{
    // Opera 8.0+, Firefox, Safari
    ajaxRequest = new XMLHttpRequest();
} catch (e){
    // Internet Explorer Browsers
    try{
        ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
        try{
            ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
        } catch (e){
            // Something went wrong
            alert("Your browser broke!");
            return false;
        }
    }
}

// Create a function that will receive data sent from the server
ajaxRequest.onreadystatechange = function(){
    if(ajaxRequest.readyState == 4){
        alert("received: " + ajaxRequest.responseText);
        var ajaxDisplay = document.getElementById('ajaxDiv');
        ajaxDisplay.innerHTML = ajaxRequest.responseText;
    }
}

ajaxRequest.open("GET", "delete_row.php" + queryString, true);
ajaxRequest.send(null); 

​</p>

于 2012-06-24T04:50:34.127 回答
0

将您的 confirm() 语句保留在 js 的顶部

window.alert = null ; 

并尝试

k让我检查一下

于 2012-06-24T04:18:48.337 回答
0

我相当确定您应该onreadystatechange在调用后设置事件open,否则处理程序将被清除。

于 2012-06-24T04:20:30.333 回答