0

复选框验证后如何停止请求过程?如果复选框从未选择过,我想停止请求。即使成功验证未选中复选框,以下脚本也不会停止该过程。

function addNewItem(id) {
var i, chks = document.getElementsByName('itemCheckbox');
for (i = 0; i < chks.length; i++){
    if (chks[i].checked){
        return true;
    }else{
        alert('No item selected');
        return false;
    }
}else{

if (window.XMLHttpRequest) {
    // code for IE7+, Firefox, Chrome, Opera, Safari
    xmlhttp=new XMLHttpRequest();
} else {
    // code for IE6, IE5
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function() {
    if (xmlhttp.readyState==4 && xmlhttp.status==200) {
        document.getElementById("itemTable").innerHTML=xmlhttp.responseText;
        initPlayWidget();
    }
}

var itemCheckbox = document.forgetMeNotForm.itemCheckbox;
var selectedSong = "";

if (typeof itemCheckbox.length === 'undefined') {
    if (itemCheckbox.checked == true) {
        selectedSong = itemCheckbox.value + ",";
    }
} else {
    for (var i=0; i < itemCheckbox.length; i++) {
        if (itemCheckbox[i].checked) {
            selectedItm = selectedItm + itemCheckbox[i].value + ",";
        }
    }
} 

var type = document.getElementById("typeDropdown").value;
var monthValue = document.getElementById("monthDropdown").value;
var dayValue = document.getElementById("dayDropdown").value;
var startTime = document.getElementById("hrDropdown1").value + document.getElementById("minDropdown1").value;
var endTime = document.getElementById("hrDropdown2").value + document.getElementById("minDropdown2").value;
var itmId = document.getElementById("itmNumber").value;

xmlhttp.open("POST","/sites/" + domainName + "/themes/web/common/myLib.php" ,true);
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xmlhttp.send("action=addItemRule&id="+id+"&selectedItm=" + selectedItm + "&type=" + type + "&monthValue=" + monthValue
    + "&dayValue=" + dayValue + "&startTime=" + startTime + "&endTime=" + endTime + "&itmId=" + itmId);

}
}

谢谢。

4

2 回答 2

0

您可以添加新功能

function isChecked(){
  var i, chks = document.getElementsByName('itemCheckbox');
  for (i = 0; i < chks.length; i++){
    if (chks[i].checked){
        return true;
    }else{
        alert('No item selected');
        return false;
    }
  }
}

并在主函数上添加新行

function addNewItem(id) {
  if (!isChecked())
    return;
  ...
于 2012-07-25T07:18:19.470 回答
0

您的代码无法编译:

function addNewItem(id) {
var i, chks = document.getElementsByName('itemCheckbox');
for (i = 0; i < chks.length; i++){
    if (chks[i].checked){
        return true;
    }else{
        alert('No item selected');
        return false;
    }
}else{
^
|

尝试删除这个大括号

编辑:你也不能有两个通用的 else 到一个 if,所以你需要稍微改变一下代码,也许把第二个 else 的内容放在 if 子句的内容中,像这样:

function addNewItem(id) {
    var i, chks = document.getElementsByName('itemCheckbox');
    for ( i = 0; i < chks.length; i++) {
        if (chks[i].checked) {
            if (window.XMLHttpRequest) {
                // code for IE7+, Firefox, Chrome, Opera, Safari
                xmlhttp = new XMLHttpRequest();
            } else {
                // code for IE6, IE5
                xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
            }
            xmlhttp.onreadystatechange = function() {
                if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                    document.getElementById("itemTable").innerHTML = xmlhttp.responseText;
                    initPlayWidget();
                }
            }
            var itemCheckbox = document.forgetMeNotForm.itemCheckbox;
            var selectedSong = "";

            if ( typeof itemCheckbox.length === 'undefined') {
                if (itemCheckbox.checked == true) {
                    selectedSong = itemCheckbox.value + ",";
                }
            } else {
                for (var i = 0; i < itemCheckbox.length; i++) {
                    if (itemCheckbox[i].checked) {
                        selectedItm = selectedItm + itemCheckbox[i].value + ",";
                    }
                }
            }

            var type = document.getElementById("typeDropdown").value;
            var monthValue = document.getElementById("monthDropdown").value;
            var dayValue = document.getElementById("dayDropdown").value;
            var startTime = document.getElementById("hrDropdown1").value + document.getElementById("minDropdown1").value;
            var endTime = document.getElementById("hrDropdown2").value + document.getElementById("minDropdown2").value;
            var itmId = document.getElementById("itmNumber").value;

            xmlhttp.open("POST", "/sites/" + domainName + "/themes/web/common/myLib.php", true);
            xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
            xmlhttp.send("action=addItemRule&id=" + id + "&selectedItm=" + selectedItm + "&type=" + type + "&monthValue=" + monthValue + "&dayValue=" + dayValue + "&startTime=" + startTime + "&endTime=" + endTime + "&itmId=" + itmId);
        } else {
            alert('No item selected');
            return false;
        } 
    }
}

Edit2:此外,如果您想在发送请求之前检查所有复选框(并且只发送一次请求,您可以使用另一个变量:

function addNewItem(id) {
    var i, chks = document.getElementsByName('itemCheckbox'), valid = true;
    for ( i = 0; i < chks.length; i++) {
        if (!chks[i].checked) {
            valid = false;
        } 
    }

    if(valid) {
        // ...
        // do the request stuff here
        // ...
    } else {
        alert('No item selected');
        return false;
    } 
}
于 2012-07-25T07:25:41.510 回答