没有返回$('input:checkbox').change(function(){? [英] No return on$('input:checkbox').change(function() {?

查看:115
本文介绍了没有返回$('input:checkbox').change(function(){?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在测试这里提供给我的第二个解决方案,用于检查和记录已选中的每个复选框.第一个解决方案的工作原理很吸引人,但是第二个更有效的解决方案甚至无法运行.

I am testing a second solution that has been provided to me here, for checking and recording each check box that has been checked. First solution works like a charm but the second more efficient solution does not even run.

for (x in aL.results) {
  aLIds.push(aL.results[x].listing_id);
  aLTitles.push(aL.results[x].title);
  aLQuantities.push(aL.results[x].quantity);
  aLDescs.push(aL.results[x].description);
  aLTaxPaths.push(aL.results[x].Tax_path);
  aLTaxIds.push(aL.results[x].Tax_id);
  aLTags.push(aL.results[x].tags);
  aLUrls.push(aL.results[x].url);
  aLPrices.push(aL.results[x].price);
  aLViews.push(aL.results[x].views);
  aLHearts.push(aL.results[x].num_favorers);

  $('#tblListings').append(
    '<tr>' + '<td><input type="checkbox" name="updateListings[]" value=' + x + ' ></td>' + '<td>' + aLQuantities[x] + '</td>' + '<td>' + aLTitles[x] + '</td>' + '<td>' + aLPrices[x] + '</td>' + '<td>' + aLViews[x] + '</td>' + '<td>' + aLHearts[x] + '</td>' + '</tr>'
  );
}

$('input:checkbox').change(function() {
    alert('ah');
    var uLIndex = $('input:checkbox:checked').map(function() {
        return this.value;
    }).get();
    console.log(uLIndex);
});

推荐答案

对于动态创建元素,我们不能直接绑定事件来执行此类操作或 jquery 官方网站

For dynamically create element we can not bind event directly do something like this or live function and this function is removed in 1.9 version so make sure what version of jquery you are using or many ways you can find out on jquery official site

$('body').on("change","input:checkbox",function() {
    alert('ah');
  var uLIndex = $('input:checkbox:checked').map(function() {
    return this.value;
  }).get();
  console.log(uLIndex);
});

已更新

将课程添加到复选框检查

$('#tblListings').append(
    '<tr>' + '<td><input class="check" type="checkbox" name="updateListings[]" value=' + x + ' ></td>' + '<td>' + aLQuantities[x] + '</td>' + '<td>' + aLTitles[x] + '</td>' + '<td>' + aLPrices[x] + '</td>' + '<td>' + aLViews[x] + '</td>' + '<td>' + aLHearts[x] + '</td>' + '</tr>'
  );

然后在类名称为 check 的所有复选框上绑定事件,如下所示.

And than bind event on all checkbox with class name check as below.

$('body').on("change",".check",function() {
    alert('ah');
  var uLIndex = $('input:checkbox:checked').map(function() {
    return this.value;
  }).get();
  console.log(uLIndex);
});

这篇关于没有返回$('input:checkbox').change(function(){?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆