如果我使用 asp.net 的 Web 用户控件,如何使用此代码工作?

发布于 2024-10-14 16:34:35 字数 1462 浏览 2 评论 0原文

如果我使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浮生面具三千个 2024-10-21 16:34:35

有几件事:

首先

  $("#sumit").click(function(){...}

是寻找 id 为“sumit”的 HTML 元素(# 表示 JQuery 中的 ID)。

如果您查看页面的源代码,您会发现 Button1 在被 ASP.Net 转换为 HTML 端按钮时不会有该 ID,因为 ASP.Net 将生成一个相当不可读的唯一客户端 Id - 充满 $ 符号的东西并可能强调。

Jquery 可以使用 $('.myClass') - '.' 按其类找到您的按钮表示“class”,就像“#”表示“ID”一样,因此只需将类 submit 添加到您的 ASP.Net Button1

<asp:Button ID="Button1" runat="server" Text="Button" CssClass='submit' />

然后修改您的 JavaScript 调用:
首先,确保事件处理程序在文档准备好时通过将 javascript 包装在标准 JQuery 语法中来完成此操作:

$(document).ready(function () {
    //Your Javascript goes in here.
});

因此,当重新工作时,它看起来像这样(我已经移动了一些东西并取出了你的</code> 元素。)

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.1/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript" language="javascript">
    $(document).ready(function () {
        $(".submit").click(function () {

            /* this code is here for demo purposes only - 5arx*/
            var numchecked = 0;

            $('input:checkbox').each(function () {
                if ($(this).attr('checked')) {
                    //alert('checked');
                    numchecked++;
                }
            });

            if (numchecked > 5) {
                alert('You cannot check more than 5 check box.');
            }

            return false; //This will stop your ASP.Net button submitting the form via a Postback.
            /* Your orginal code.
            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.');

            }
            */
        });
    });
</script>
<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" CssClass="submit" />
</div>

希望这会有所帮助。有关更多有用的 JQuery 内容,请访问 www.visualjquery.com - 它是一个交互式 JQuery 测试/学习工具。

哈。祝你好运 :-)

Couple of things:

First

  $("#sumit").click(function(){...}

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.Net Button1:

<asp:Button ID="Button1" runat="server" Text="Button" CssClass='submit' />

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:

$(document).ready(function () {
    //Your Javascript goes in here.
});

So when re-worked it looks like this (I've moved a few things around and taken out your <title> element.)

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.1/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript" language="javascript">
    $(document).ready(function () {
        $(".submit").click(function () {

            /* this code is here for demo purposes only - 5arx*/
            var numchecked = 0;

            $('input:checkbox').each(function () {
                if ($(this).attr('checked')) {
                    //alert('checked');
                    numchecked++;
                }
            });

            if (numchecked > 5) {
                alert('You cannot check more than 5 check box.');
            }

            return false; //This will stop your ASP.Net button submitting the form via a Postback.
            /* Your orginal code.
            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.');

            }
            */
        });
    });
</script>
<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" CssClass="submit" />
</div>

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 :-)

錯遇了你 2024-10-21 16:34:35

$("#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").

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文