1

以下尝试在 IE8 中有效,但在 Firefox 中无效(不能为此使用 JQuery):

case 'Template:templateControl:residenceRBL2': 
    if (selected.value == 'Within USA') 
    {
        /* Enable zip textbox and validator */
 document.getElementById("Template_templateControl_zipTxt1").disabled=false;
 ValidatorEnable(document.getElementById('<%=firstPersonZipReqVal.ClientID%>'),
            true);
 ...
    }
    else if (selected.value == 'Outside USA') 
    {
        /* Disable zip textbox and validator */
 document.getElementById("Template_templateControl_zipTxt1").disabled=true;
 ValidatorEnable(document.getElementById('<%=firstPersonZipReqVal.ClientID%>'),
            false);
 ...
    }
    break;

    <asp:Label ID="firstPersonZipLabel" Runat="server"></asp:Label><br />
 <asp:TextBox ID="zipTxt1" Height="19" Width="100" 
        Runat="server"></asp:TextBox>
    <asp:RequiredFieldValidator ID="firstPersonZipReqVal" 
        ControlToValidate="zipTxt1" Display="Dynamic" 
            ErrorMessage="First Person Zip" 
                Runat="server">*</asp:RequiredFieldValidator>

问题描述:根据单选按钮列表选择,为文本框禁用/重新启用所需的验证器。基本上,如果他们的地址在美国境外,那么 zip 文本框验证器将被禁用。在 Firefox 中,这根本不起作用。

更新 1:09-07-2010 我在 Firefox 中禁用了文本框;我使用的是名称属性而不是 id。我现在唯一的问题是如何在 JS 中访问 ASP.NET 验证器控件的“ClientID”?

更新 2:09-07-2010 根据MSDN 文档,我想我可以这样做:

ValidatorEnable(firstPersonZipReqVal, false);

不幸的是,这似乎在 Firefox 中也不起作用......

ASP.NET 在 Firefox 中破坏了验证客户端代码的呈现

更新 3:2010 年 9 月 8 日

Firefox 表现不佳的原因是 ASP.NET 1.1 将其视为低级浏览器。如果我将 clientTarget="upLevel" 放在 Page 标记中,Firefox 会按预期工作。不幸的是,这破坏了整个网站的布局。有没有更渐进的方法来修复 Firefox 的 browsercaps? 这个版本的 browserCaps 也破坏了布局。

Web.Config 中的当前 browserCaps 如下所示:

 <browserCaps> 
  <case match="Gecko/[-\d]+">
   browser=Netscape
   frames=true
   tables=true
   cookies=true
   javascript=true
   javaapplets=true
   ecmascriptversion=1.5
   w3cdomversion=1.0
   css1=true
   css2=true
   xml=true
   tagwriter=System.Web.UI.HtmlTextWriter
   <case match="rv:1.0[^\.](?'letters'\w*)">
    version=6.0
    majorversion=6
    minorversion=0
    <case match="^b" with="${letters}">
     beta=true
    </case>
   </case>
   <case match="rv:1(\.\d+)(\.\d)?(?'letters'\w*)">
    version=7.0
    majorversion=7
    minorversion=0
    <case match="^b" with="${letters}">
     beta=true
    </case>
   </case>
  </case>
 </browserCaps>

更新:2010 年 9 月 11 日

以下链接可能会提供答案;有人可以帮助提供 50 分的代码吗?

http://www.4guysfromrolla.com/articles/051204-1.aspx

4

5 回答 5

1

如何在 JS 中访问 ASP.NET 验证器控件的“ClientID”

您在代码中以正确的方式访问它,尽管您可能需要使用双引号而不是单引号:

document.getElementById("<%=firstPersonZipReqVal.ClientID%>")
于 2010-09-07T17:08:56.510 回答
0

好吧,问题很老了,但 fr1.1 仍然存在 - 实际上 Mitchell 的文章没有提供解决方法,让我展示一下我的:
1. Web config browserCaps 应该包含“msdomversion”,如下所示:

        <browserCaps>
        <case match="^Mozilla/5\.0 \([^)]*\) (Gecko/[-\d]+)(?'VendorProductToken' (?'type'[^/\d]*)([\d]*)/(?'version'(?'major'\d+)(?'minor'\.\d+)(?'letters'\w*)))?">
            browser=Gecko
            <filter>
                <case match="(Gecko/[-\d]+)(?'VendorProductToken' (?'type'[^/\d]*)([\d]*)/(?'version'(?'major'\d+)(?'minor'\.\d+)(?'letters'\w*)))">
                type=${type}
                </case>
                <case>
                    <!-- plain Mozilla if no VendorProductToken found -->
                type=Mozilla
                </case>
            </filter>
            frames=true
            tables=true
            cookies=true
            javascript=true
            javaapplets=true
            ecmascriptversion=1.2 
            w3cdomversion=1.0
            css1=true
            css2=true
            xml=true
            msdomversion=6.0
            tagwriter=System.Web.UI.HtmlTextWriter
            <case match="rv:(?'version'(?'major'\d+)(?'minor'\.\d+)(?'letters'\w*))">
                version=${version}
                majorversion=0${major}
                minorversion=0${minor}
                <case match="^b" with="${letters}">
                    beta=true
                </case>
            </case>
        </case>
        <case match="Chrome/(?'version'(?'major'\d+)\.(?'minor'\d+\.\d+).\d+)">
            browser=Chrome
            version=${version}
            majorversion=${major}
            minorversion=${minor}
            frames=true
            tables=true
            cookies=true
            javascript=true
            javaapplets=true
            ecmascriptversion=1.5
            w3cdomversion=1.0
            msdomversion=6.0
            css1=true
            css2=true
            xml=true
            tagwriter=System.Web.UI.HtmlTextWriter
        </case>
    </browserCaps>

2. WebUIValidation.js 应该修改:
2.1 用 document.getElementById (Mitchell) 替换对 document.all 的调用
2.2 重命名变量“final” (Mitchell)
2.3 添加函数以读取 expando 作为属性:

function ValidatorValidatorGetAttribute(item, attribName)
{
    var retVal = null;
    var attribs = item.attributes;
    for (var i = attribs.length - 1; i >= 0; i--)
    {
        var attrib = attribs[i];
        if (attrib.nodeName == attribName)
        {
            retVal = attrib.nodeValue;
        }
    }
    return retVal;
}

2.4 添加expando处理逻辑,例如在ValidatorOnLoad中:

if (typeof(val.evaluationfunction) == "string") {
    eval("val.evaluationfunction = " + val.evaluationfunction + ";");
}
else
if (typeof(ValidatorGetAttribute(val,'evaluationfunction')) == "string") {
    eval("val.evaluationfunction = " + ValidatorGetAttribute(val,'evaluationfunction') + ";");
}

3.在页面上添加客户端脚本(自动脚本检查它是否仅在IE浏览器上运行)

var nonIEBrowser = false;
if (typeof(clientInformation) == "undefined")
    nonIEBrowser = true;
if ((typeof(clientInformation) != "undefined") && (clientInformation.appName.indexOf("Explorer") == -1))
    nonIEBrowser = true;
//None - IE browsers, and possibly IE 10+ 
if (nonIEBrowser) 
{
    if (typeof(Page_ValidationVer) == "undefined")
        alert("Unable to find script library '/aspnet_client/system_web/1_1_4322/WebUIValidation.js'. Try placing this file manually, or reinstall by running 'aspnet_regiis -c'.");
    else if (Page_ValidationVer != "125")
        alert("This page uses an incorrect version of WebUIValidation.js. The page expects version 125. The script library is " + Page_ValidationVer + ".");
    else
        ValidatorOnLoad();
}

就这样。至少它对我有用:)

于 2013-12-06T10:07:40.900 回答
0

标记为答案 - 请参阅上述 Scott Mitchell 文章的链接。

于 2010-09-13T15:41:27.257 回答
0

我相信这里的问题是您在 DOM 准备好之前调用了 Javascript ,所以这就是为什么无法使用 getElementByID 找到您的元素的原因。

有2个解决方案。

  1. 在页面底部呈现您的 JavaScript,至少在您的控件之后
  2. 将您的 Javascript 调用放在一个函数上,并首先使用window.onload调用它。

当然,您需要使用 <%=firstPersonZipReqVal.ClientID%> 通过 palswim 建议给他们打电话

我不是 100% 认为这是问题,因为我没有看到完整的代码,但这是 getElementByID 找不到现有控件的主要原因。

于 2010-09-08T08:16:55.883 回答
0

这太长了,无法添加到评论中,并且是我上面帖子中答案的一个单独的刺。

我直接从有关此问题的帖子中复制并粘贴此内容,但用户尝试了以下操作:

function disableValidator(elem)
{
    elem.style.cssText = "";
    elem.style.display = 'none';
    elem.style.accelerator = true;
}

disableValidator( document.getElementById("<%=firstPersonZipReqVal.ClientID%>") );
于 2010-09-07T21:44:32.803 回答