【问题标题】:ASP.NET 2.0 Custom Client Side Validation not working in Internet ExplorerASP.NET 2.0 自定义客户端验证在 Internet Explorer 中不起作用
【发布时间】:2012-03-26 11:03:02
【问题描述】:

我有两个文本框,一个用于预定日期,另一个用于预定时间。如果两个文本框都是空白的,或者都有内容,我想通过验证。如果只有一个有内容,我想验证失败。服务器端一切正常,以下客户端代码在 Chrome 中正常运行。

    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 中被破坏了,如果我再次尝试进入,它会再次中断,但是如果我第三次尝试进入它,它会运行,并且验证工作正常.

有人能解释一下吗?

【问题讨论】:

  • 你在使用 document.getElementById('ctl00_MainContent_txtScheduledTime') 吗?
  • 这段代码实际上取自浏览器渲染的代码,我在你看到ctl00_MainContent_txtScheduledTime.value的所有地方都使用<%=txtScheduledTime.ClientID%>.value,对于日期字段也是如此。我没有使用 document.getElementById。
  • 更改为 if (document.getElementById('<%=txtScheduledTime.ClientID%>').value!="" || document.getElementById('<%=txtScheduledDate.ClientID%>').value!="" ) 非常有用,谢谢!
  • 您想将其添加为答案以便我接受吗?

标签: javascript asp.net validation c#-2.0 custom-validators


【解决方案1】:

你可以这样使用它

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

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

或者你可以像这样使用它

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

问候。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-19
    • 2021-09-24
    相关资源
    最近更新 更多