2012-03-26 129 views
0

我有两个文本框,一个用于预定日期,另一个用于预定时间。如果两个文本框都是空白的,或者两者都有内容,我想通过验证。如果只有一个人有内容,我想验证失败。所有工作都很好,服务器端,以下客户端代码在Chrome中正常工作。ASP.NET 2.0自定义客户端验证在Internet Explorer中不起作用

function CheckScheduledDateTime(sender, args) { 
     if (ctl00_MainContent_txtScheduledTime.value!="" || ctl00_MainContent_txtScheduledDate.value!="") 
     { 
      if (ctl00_MainContent_txtScheduledTime.value!="" && ctl00_MainContent_txtScheduledDate.value=="") 
      { 
       args.IsValid=false; 
       alert("Scheduled date is required"); 
      } 
      else if (ctl00_MainContent_txtScheduledTime.value=="" && ctl00_MainContent_txtScheduledDate.value1!="") 
      { 
       args.IsValid=false; 
       alert("Scheduled time is required"); 
      } 
      else 
      { 
       args.IsValid=true; 
      } 

     } 
     else 
     { 
      args.IsValid = true; 
     }   
    } 

在Internet Explorer中,这是行不通的,而我收到以下错误:

“Microsoft JScript运行错误:”ctl00_MainContent_txtScheduledTime未定义“

奇怪的是,与它会在Visual Studio中崩溃,如果我再尝试再次进入,它会再次中断,但如果我尝试第三次尝试并进入它,并且验证正常工作。

有没有人可以对此有所了解?

+1

你使用document.getElementById('ctl00_MainContent_txtScheduledTime')? – 2012-03-26 11:06:50

+0

这段代码实际上是从浏览器呈现的代码中提取的,我在所有看到'ctl00_MainContent_txtScheduledTime.value'的地方都使用了'<%= txtScheduledTime.ClientID%> .value',同样也是日期字段。我没有使用document.getElementById。 – fourdam 2012-03-26 11:13:40

+0

更改为'if(document.getElementById('<%= txtScheduledTime.ClientID%>')。value!=“”|| document.getElementById('<%= txtScheduledDate.ClientID%>')。value!=“” )'已经工作过了,谢谢! – fourdam 2012-03-26 11:20:26

回答

1

您可以使用它像这样

ctl00_MainContent_txtScheduledTime不是JavaScript的变量,直到您初始化它们使用

var ctl00_MainContent_txtScheduledTime = document.getElementById('<%=txtScheduledTime.ClientID%>'); 

,或者您可以使用它像

(document.getElementById('<%=txtScheduledTime.ClientID%>').value!="" || document.getElementById('<%=txtScheduledDate.ClientID%>').value!="") 

问候。