1

如何通过Jquery将CheckBox名称添加到Div control复选框状态为真时Checked

我试过下面的代码。但没有得到结果。

ASP 代码:

<div align="center" id="chkBoxes">
    <asp:CheckBox ID="chbIceCream" Text ="Ice Cream" runat="server" />
    <asp:CheckBox ID="chbCake" Text ="Cake" runat="server" />
    <asp:CheckBox ID="chbChocolet" Text ="Cho colet" runat="server" />
</div>

<div id="ContentDiv">


</div>

jQuery代码:

$(document).ready(function () {
    $('#chbIceCream').click(function () {
        debugger;
        var Text = $('#chbIceCream').text();
        if ($(this).attr('checked'))
            $('#ContentDiv').append(Text);
    });
});

如果有人帮助,请。

4

2 回答 2

3

如果您想在 javascript 中使用服务器端 id 或在 javascript 中使用 CliendID,请设置您的控件ClientIDMode="static" 。

使用 ClientIDMode="static"

<asp:CheckBox ID="chbIceCream" Text ="Ice Cream" runat="server" ClientIDMode="static" />

控件 ID 的 jQuery 选择器没有变化

$(document).ready(function () {
    $('#chbIceCream').click(function () {
        debugger;
        var Text = $('#chbIceCream').text();
        if ($(this).attr('checked'))
            $('#ContentDiv').append(Text);
    });
});

使用客户端 ID

$(document).ready(function () {
  $('#<%= chbIceCream.ClientID %>').click(function () {
    debugger;
    var Text = $('#chbIceCream').text();
    if ($(this).attr('checked'))

        $('#ContentDiv').append(Text);
   })
});
于 2012-12-26T06:57:06.017 回答
0

使用它作为选择器

 $('#<%= chbIceCream.ClientID %>').click(function(){
  ....

更新

添加ClientIDMode="static"你的html

于 2012-12-26T06:58:31.260 回答