如果我使用 asp.net 的 Web 用户控件,如何使用此代码工作?
如果我使用 asp.net 的 Web 用户控件,如何使用此代码工作?
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:CheckBox ID="CheckBox1" runat="server" /><br />
<asp:CheckBox ID="CheckBox2" runat="server" /><br />
<asp:CheckBox ID="CheckBox3" runat="server" /><br />
<asp:CheckBox ID="CheckBox4" runat="server" /><br />
<asp:CheckBox ID="CheckBox5" runat="server" /><br />
<asp:CheckBox ID="CheckBox6" runat="server" /><br />
<asp:CheckBox ID="CheckBox7" runat="server" /><br />
<asp:CheckBox ID="CheckBox8" runat="server" /><br />
<asp:CheckBox ID="CheckBox9" runat="server" /><br />
<asp:CheckBox ID="CheckBox10" runat="server" /><br />
<asp:Button ID="Button1" runat="server" Text="Button" />
</div>
</form>
<script src ="http://ajax.googleapis.com/ajax/libs/jquery/1.3.1/jquery.min.js"></script>
<title>Untitled Page</title>
<script type="text/javascript">
$("#sumit").click(function(){
var vCheckedCBCount = $("input:checkbox").filter(function(index){
return $(this)[0].checked == true;
}).length;
if(vCheckedCBCount > 5)
{
alert('You cannot check more than 5 check box.');
return false;
}
});
</script >
</body>
</html>
How to use this code working if i use web user control of asp.net ?
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:CheckBox ID="CheckBox1" runat="server" /><br />
<asp:CheckBox ID="CheckBox2" runat="server" /><br />
<asp:CheckBox ID="CheckBox3" runat="server" /><br />
<asp:CheckBox ID="CheckBox4" runat="server" /><br />
<asp:CheckBox ID="CheckBox5" runat="server" /><br />
<asp:CheckBox ID="CheckBox6" runat="server" /><br />
<asp:CheckBox ID="CheckBox7" runat="server" /><br />
<asp:CheckBox ID="CheckBox8" runat="server" /><br />
<asp:CheckBox ID="CheckBox9" runat="server" /><br />
<asp:CheckBox ID="CheckBox10" runat="server" /><br />
<asp:Button ID="Button1" runat="server" Text="Button" />
</div>
</form>
<script src ="http://ajax.googleapis.com/ajax/libs/jquery/1.3.1/jquery.min.js"></script>
<title>Untitled Page</title>
<script type="text/javascript">
$("#sumit").click(function(){
var vCheckedCBCount = $("input:checkbox").filter(function(index){
return $(this)[0].checked == true;
}).length;
if(vCheckedCBCount > 5)
{
alert('You cannot check more than 5 check box.');
return false;
}
});
</script >
</body>
</html>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
有几件事:
首先
是寻找 id 为“sumit”的 HTML 元素(# 表示 JQuery 中的 ID)。
如果您查看页面的源代码,您会发现 Button1 在被 ASP.Net 转换为 HTML 端按钮时不会有该 ID,因为 ASP.Net 将生成一个相当不可读的唯一客户端 Id - 充满 $ 符号的东西并可能强调。
Jquery 可以使用 $('.myClass') - '.' 按其类找到您的按钮表示“class”,就像“#”表示“ID”一样,因此只需将类
submit
添加到您的 ASP.NetButton1
:然后修改您的 JavaScript 调用:
首先,确保事件处理程序在文档准备好时通过将 javascript 包装在标准 JQuery 语法中来完成此操作:
因此,当重新工作时,它看起来像这样(我已经移动了一些东西并取出了你的</code> 元素。)
希望这会有所帮助。有关更多有用的 JQuery 内容,请访问 www.visualjquery.com - 它是一个交互式 JQuery 测试/学习工具。
哈。祝你好运 :-)
Couple of things:
First
Is looking for an HTML element with an id of 'sumit' (the # means ID in JQuery).
If you view the source of your page you'll see that Button1 when converted by ASP.Net into an HTML-side button won't have that ID as ASP.Net will generate a fairly unreadable unique clientside Id - something full of $ signs and underscores probably.
Jquery can find your button by its class using $('.myClass') - the '.' means 'class' just as '#' means 'ID' so just add the class
submit
to your ASP.NetButton1
:Then modify your JavaScript call:
Firstly, ensure the eventhandlers are getting wired up when the document is ready by wrapping up your javascript inside the standard JQuery syntax to do this:
So when re-worked it looks like this (I've moved a few things around and taken out your
<title>
element.)Hope this helps. For more useful JQuery stuff, have a look at www.visualjquery.com - its an interactive JQuery testing/learning tool.
Hth. Good luck :-)
$("#sumit")
正在查找 ID="sumit" 的元素,而您的元素都没有该元素。假设您想挂钩按钮的点击事件,请尝试$("#Button1")
。$("#sumit")
is looking for an element with ID="sumit", which neither of your elements have. Assuming you want to hook your button's click event, try$("#Button1")
.