多个条件没有在javascript中触发

时间:2013-03-26 13:04:28

标签: javascript

我正在编写javascript函数来检查aspx页面的下拉值是否具有“已​​完成”或“已取消”的值。如果是,则检查日期和时间是否为空。 但功能永远不会发生。代码如下。

function EnableValidator() {
    var drp = document.getElementById('<%=drpcallstatus.ClientID %>');
    var txt = drp.options[drp.selectedIndex].text;
    var dt = document.getElementById('<%=txtcompletedate.ClientID %>');
    var ct = document.getElementById('<%=txtcomptime.ClientID %>');



    if ((txt == "Completed" | txt=="Cancelled") && (dt===null | ct===null))  {

            alert("Please Enter the Completed Date and Time");

            return false;

    }

该函数从asp.net按钮

调用
 <asp:Button ID="btnsubmit" runat="server" Text="Submit"  OnClientClick="return EnableValidator()"  onclick="btnsubmit_Click" />

2 个答案:

答案 0 :(得分:1)

此处的问题是您没有为OR使用正确的运算符。

if ((txt == "Completed" || txt=="Cancelled") && (dt===null || ct===null))  {
  ...
}

在条件表达式中为||运算符使用双管道字符OR

答案 1 :(得分:1)

尝试:

if ((txt == "Completed" || txt=="Cancelled") && (dt===null || ct===null))  {
            alert("Please Enter the Completed Date and Time");
            return false;
}

你必须使用||才能正常工作。