【问题标题】:How to append text to Div form jQuery in asp.net?如何在asp.net中将文本附加到Div表单jQuery?
【发布时间】:2012-12-26 15:34:47
【问题描述】:

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

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

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);
    });
});

如果有人帮忙,请。

【问题讨论】:

    标签: jquery asp.net html checkbox


    【解决方案1】:

    如果您想在 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);
        });
    });
    

    使用 ClientID

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

    【讨论】:

    • 我正在为单独的 js 文件 @Adil 使用脚本
    • 它填充此错误 @Adil 'Microsoft JScript 运行时错误:语法错误,无法识别的表达式:#'
    • 是的,它应该给出错误我已经更新了我的答案,您需要使用 ClientIDMode="static" 的第一个选项
    • 我仍然遇到同样的错误@Adil
    • 使用 ClientIDMode 你不需要#'
    【解决方案2】:

    将其用作选择器

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

    更新

    在您的 html 中添加 ClientIDMode="static"

    【讨论】:

    • 它填充了这个错误@bipen 'Microsoft JScript 运行时错误:语法错误,无法识别的表达式:#'
    猜你喜欢
    • 1970-01-01
    • 2011-08-06
    • 2017-11-18
    • 2012-06-19
    • 2017-06-14
    • 1970-01-01
    • 2014-11-05
    • 2011-06-02
    相关资源
    最近更新 更多