是否有跨浏览器方式在expando属性上使用jQuery选择器?

时间:2010-09-02 13:58:07

标签: asp.net jquery jquery-selectors expando

我有一个ASP.NET页面,我试图使用jQuery选择器快速匹配绑定到特定文本框(文本输入)的验证控件。验证控件呈现为span,“controltovalidate”属性呈现为expando属性。这是一个测试示例:

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
    <script type="text/javascript" src="jquery.js"></script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="TextBox1"
            ErrorMessage="RequiredFieldValidator"></asp:RequiredFieldValidator>

        <asp:Button ID="Button1" runat="server" Text="Button" /></div>

        <input type="button" value="Test" onclick="ml_test();" />

        <script type="text/javascript">
            ml_test = function() {
                alert($('[controltovalidate=TextBox1]').get().length);
            };
        </script>

    </form>
</body>
</html>

呈现为:

(code removed here)

        <input name="TextBox1" type="text" id="TextBox1" />
        <span id="RequiredFieldValidator1" style="color:Red;visibility:hidden;">RequiredFieldValidator</span>
        <span id="RegularExpressionValidator1"></span>

        <input type="submit" name="Button1" value="Button" onclick="javascript:WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions(&quot;Button1&quot;, &quot;&quot;, true, &quot;&quot;, &quot;&quot;, false, false))" id="Button1" /></div>

        <input type="button" value="Test" onclick="ml_test();" />

        <script type="text/javascript">
            ml_test = function() {
                alert($('[controltovalidate=TextBox1]').get().length);
            };
        </script>

(code removed here)

<script type="text/javascript">
//<![CDATA[
var RequiredFieldValidator1 = document.all ? document.all["RequiredFieldValidator1"] : document.getElementById("RequiredFieldValidator1");
RequiredFieldValidator1.controltovalidate = "TextBox1";
RequiredFieldValidator1.errormessage = "RequiredFieldValidator";
RequiredFieldValidator1.evaluationfunction = "RequiredFieldValidatorEvaluateIsValid";
RequiredFieldValidator1.initialvalue = "";
//]]>
</script>

问题:ml_test()函数在Internet Explorer 7中显示为1(如预期的那样),但在Firefox 3.6.8中显示为0。我尝试添加其他控件,但在Firefox中它始终不起作用。

我发现this post显示在选择器中使用&符号,如[@expando=value],但是当我尝试这种语法时,jQuery 1.4.2会抛出错误。

是否有跨浏览器方式来选择expando属性,如果有,那么正确的语法是什么?

2 个答案:

答案 0 :(得分:1)

您可以像这样使用.filter()

ml_test = function() {
    alert($('span').filter(function() { return this.controltovalidate=='TextBox1'; }).length);
};

You can test it here,或者将其设为自定义选择器,如下所示:

jQuery.expr[':'].validatorFor = function(o, i, m){
  return o.controltovalidate === m[3];
};​

并称之为:

ml_test = function() {
    alert($('span:validatorFor(TextBox1)').length);
};

You can that that version here

答案 1 :(得分:0)

您是否正在寻找显示验证消息的范围,如果无效?

若是,请尝试:(使用jQuery)

ml_test = function() {
    alert($('RequiredFieldValidator1').length);
};

如果你还希望能够看到它正在验证哪种控件,请执行以下操作:

ml_test = function() {

    // getting the control with jQuery by its ID.  .Net already has it
    // defined in the script block that is generated by the 
    // RequiredFieldValidator control
    var validateCtrl = $(RequiredFieldValidator1.id);  

    // get the value from the .Net-defined validator variable
    var controlToValidate = RequiredFieldValidator1.controltovalidate;

    // alert the values:
    alert('RequiredValidatorControl count: ' + validator.length + 
          '\nControl To Validate: ' + controlToValidate);

};