【问题标题】:How to change image on dynamic checkboxes?如何更改动态复选框上的图像?
【发布时间】:2018-05-23 16:34:53
【问题描述】:

我是 JQuery 的新手,非常感谢一些帮助! 我有动态复选框,上面有一个图标和图像(复选框被隐藏)...

 plcUserReq.Controls.Add(new LiteralControl("
<div data-toggle='buttons' style='margin-top:-3px'>
  <label class='btn' for='Lines' style='padding:0px 0px 0px 0px; margin:0px 0px 0px 0px'>
     <input type='checkbox' autocomplete='off' name='chkbx' value='" + UID.ToString() + "'id='chk'"+ UID.ToString() + i.ToString() +"/>
           <i class='btn fa fa-plus-square-o' aria-hidden = 'true'>
                 <img src='http://localhost:3162/images/moderator-red.gif'></i></label></div>"));

我希望图像和图标在点击时改变......当每个复选框被点击时......

这是我正在尝试的脚本...

$("[name='chkbx']").change(function () {
        if ($(this).is(':checked'))
            {
             $(this).next().find('i').attr('class', 'btn fa fa-check-square-o');
             $(this).next().find('img').attr('src', 'http://localhost:3162/images/moderator-red.gif');
            }
        else
        {
            $(this).next().find('i').attr('class', 'btn fa fa-plus-square-o');
            $(this).next().find('img').attr('src', 'http://localhost:3162/images/moderator-blue.gif');
        }
});

当任何复选框被点击时什么都没有发生......我似乎无法弄清楚我的 JQuery 有什么问题...... 请帮忙!我被困住了!

JSFiddlehttps://jsfiddle.net/Lgxacm9m/3/

选中后(应如下所示):https://jsfiddle.net/m278c236/1/

【问题讨论】:

    标签: jquery checkbox dynamic


    【解决方案1】:

    在 jQuery 中,# 用于通过某个id 来标识元素。您的复选框没有chkbxid,而是chkbxname。所以:

    $("#chkbx").change(function() {

    应该是:

    $("[name='chkbx']").change(function () {

    编辑:如果事件仍未触发,则您的事件处理程序可能会在复选框元素加载到 DOM 之前尝试绑定。您可以通过确保代码仅在整个页面呈现后才执行来解决此问题。为此,请按如下方式编辑您的代码:

    $(function() {
       $(document).on("change", "[name='chkbx']", change(function () {
          alert("At least the event is firing...");
          if ($(this).is(':checked'))
            {
              $(this).next().find('i').attr('class', 'btn fa fa-check-square-o');
              $(this).next().find('img').attr('src', 'http://localhost:3162/images/moderator-red.gif');
            }
            else
            {
              $(this).next().find('i').attr('class', 'btn fa fa-plus-square-o');
              $(this).next().find('img').attr('src', 'http://localhost:3162/images/moderator-blue.gif');
            }
       });
    });
    

    $(function() { })$(document).ready() 的简写

    【讨论】:

    • 是的,我注意到了这个错误……我也使用了 prev() 而不是 next()……请检查编辑。还是不行!
    • 当你说它不起作用时,你能详细说明一下吗?事件没有触发吗?您可以通过添加 alert("It's firing at least."); 作为事件处理程序中的第一行代码(在您的 if 块之前)来验证它是否正在触发。
    • 请看我的编辑。如果即使在实施更改之后它仍然没有触发,我猜你的内容正在异步添加到 DOM 中。另一种可能性是 JavaScript 代码中的其他语法错误。无论哪种情况,请发布您的完整 JavaScript 代码,我可以进一步提供帮助。
    • 仍然,不行..复选框是动态生成的(根据数据库中的行),我已经在我的母版页中添加了这个脚本..(在源代码中......在脚本标签中)
    • 我建议您检查一下您是否有其他 JavaScript 错误,这些错误可能会阻止您的事件处理程序甚至首先绑定。您可以通过按 F12 调出浏览器的开发人员工具来查看浏览器中的错误。开发人员工具窗格的“控制台”将报告任何此类错误。
    猜你喜欢
    • 1970-01-01
    • 2011-12-08
    • 1970-01-01
    • 2015-04-18
    • 1970-01-01
    • 1970-01-01
    • 2015-06-12
    • 2017-11-17
    相关资源
    最近更新 更多