Javascript条件无法按预期工作

时间:2016-10-17 07:41:10

标签: javascript if-statement

我有一个JS代码,可以在

时正常工作
checkQueryString != "M"

但当值变为checkQueryString == "M"时,它不会进入循环

这是我的代码。

function GridExpInfo_ClientAdd(record) {
        var checkQueryString = '<%= Request.QueryString["Mode"] %>';    
        if (checkQueryString != "M") {
            if ($('input:checked').length > 0) {

                document.getElementById("GridExpInfo_tplRowEdit3_ctl00_txtExpRefNo").disabled = true;
                document.getElementById("GridExpInfo_tplRowEdit3_ctl00_txtExpRefDt").disabled = true;

                document.getElementById('GridExpInfo_tplRowEdit3_ctl00_ddlStageType').value = "";
                var last_value = 0;
                var last_text;

                var checkboxlist = document.getElementById('ddlStatus');
                var checkOptions =   checkboxlist.getElementsByTagName('input');
                var listSelected = checkboxlist.getElementsByTagName('label');

                for (i = 0; i < checkOptions.length; i++) {
                    if (checkOptions[i].checked == true) {
                        last_text = listSelected[i].innerText;
                        last_value =  checkOptions[i].value;
                        document.getElementById('GridExpInfo_tplRowEdit3_ctl00_ddlStageType').innerHTML = "";
                        var ObjPriOptionExp = document.createElement("OPTION");
                        ObjPriOptionExp.text = last_text;                    
                        ObjPriOptionExp.value = last_value;
                        document.getElementById('GridExpInfo_tplRowEdit3_ctl00_ddlStageType').add(ObjPriOptionExp);
                    }
                }
            }
            if(checkQueryString == "M")
            {
               alert('Value is M now'); 
            }
            else {
                alert('Kindly select the stage');
            }
        }
    }

所以我的问题是,当ifM匹配时,为什么它不会进入ExcelValues

1 个答案:

答案 0 :(得分:1)

因为

if (checkQueryString == "M") {
    alert('Value is M now');
} else {
    alert('Kindly select the stage');
}

if (checkQueryString == "M")

所以试试这个

function GridExpInfo_ClientAdd(record) {
    var checkQueryString = '<%= Request.QueryString["Mode"] %>';
    if (checkQueryString != "M") {
        if ($('input:checked').length > 0) {

            document.getElementById("GridExpInfo_tplRowEdit3_ctl00_txtExpRefNo").disabled = true;
            document.getElementById("GridExpInfo_tplRowEdit3_ctl00_txtExpRefDt").disabled = true;

            document.getElementById('GridExpInfo_tplRowEdit3_ctl00_ddlStageType').value = "";
            var last_value = 0;
            var last_text;

            var checkboxlist = document.getElementById('ddlStatus');
            var checkOptions = checkboxlist.getElementsByTagName('input');
            var listSelected = checkboxlist.getElementsByTagName('label');

            for (i = 0; i < checkOptions.length; i++) {
                if (checkOptions[i].checked == true) {
                    last_text = listSelected[i].innerText;
                    last_value = checkOptions[i].value;
                    document.getElementById('GridExpInfo_tplRowEdit3_ctl00_ddlStageType').innerHTML = "";
                    var ObjPriOptionExp = document.createElement("OPTION");
                    ObjPriOptionExp.text = last_text;
                    ObjPriOptionExp.value = last_value;
                    document.getElementById('GridExpInfo_tplRowEdit3_ctl00_ddlStageType').add(ObjPriOptionExp);
                }
            }
        }

    } else if (checkQueryString == "M") {
        alert('Value is M now');
    } else {
        alert('Kindly select the stage');
    }
}