我有一个ASP.NETpage和我正在尝试使用jQuery选择器快速匹配绑定到特定文本框(文本输入)的验证控件。验证控件呈现为范围,“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("Button1", "", true, "", "", 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()函数在InternetExplorer7中显示1(如预期的那样),但在Firefox3.6.8中显示0。我试着添加额外的控件,但在Firefox中总是不起作用。
我发现
this post
它显示了在选择器中像这样使用符号
[@expando=value]
,但当我尝试这种语法时,jQuery1.4.2会抛出一个错误。