Javascript 单击同一按钮类上的事件,但在jquery中调用不同的操作

Javascript 单击同一按钮类上的事件,但在jquery中调用不同的操作,javascript,jquery,Javascript,Jquery,我有个问题。我在这里定义了代码: $('.js cars item[type=“checkbox”]')。更改(函数(){ var idx=$(this).closest('li').index();//按顺序获取索引编号 var chk=$(this).is(“:checked”);//如果选中或不选中,则获取 var obj=this;//复选框对象 $('.js cars item').each(function(){//循环每个js cars项 //找到与单击的复选框索引相同的复选框。

我有个问题。我在这里定义了代码:

$('.js cars item[type=“checkbox”]')。更改(函数(){
var idx=$(this).closest('li').index();//按顺序获取索引编号
var chk=$(this).is(“:checked”);//如果选中或不选中,则获取
var obj=this;//复选框对象
$('.js cars item').each(function(){//循环每个js cars项
//找到与单击的复选框索引相同的复选框。更改禁用属性
$(this.find('li:eq('+idx+')[type=“checkbox”]')).not(obj.prop(“checked”,false);
});
var checkeds=[];
$(“.cars项输入:复选框:选中”)。每个(函数(索引){
checkeds[index]=$(this.attr('id');
});
console.clear();
log(“这些已检查:”,已检查);
})
$('.js添加类别')。单击(函数(){
变量类别内容=`
添加节
` $('.main').append(categoryContent); }); $(document.body).on('click','button.js add section',function()){ var sectionContent=`
  • $(“.cars”)
    指的是所有
    .cars
    元素。您只需要包含您单击的按钮的元素。这将是
    $(this)。最近的(.cars”)

    要使验证代码在新添加的节上工作,需要使用事件委派,就像对
    .js add节
    单击处理程序所做的那样

    $(“.main”).on('change','.js cars项[type=“checkbox”]”,函数(){
    var idx=$(this).closest('li').index();//按顺序获取索引编号
    var chk=$(this).is(“:checked”);//如果选中或不选中,则获取
    var obj=this;//复选框对象
    $(this).closest('.cars').find('.js cars item').each(function(){//Loop every js cars item
    //找到与单击的复选框索引相同的复选框。更改禁用属性
    $(this.find('li:eq('+idx+')[type=“checkbox”]')).not(obj.prop(“checked”,false);
    });
    var checkeds=[];
    $(this).closest(“.cars”).find(“.cars项目输入:复选框:选中”).each(函数(索引){
    checkeds[index]=$(this.attr('id');
    });
    console.clear();
    log(“这些已检查:”,已检查);
    })
    $('.js添加类别')。单击(函数(){
    变量类别内容=`
    
    添加节
    ` $('.main').append(categoryContent); }); $(document.body).on('click','button.js add section',function()){ var sectionContent=`
    • $(“.cars”)
      指的是所有
      .cars
      元素。您只需要包含您单击的按钮的元素。这将是
      $(this)。最近的(.cars”)

      要使验证代码在新添加的节上工作,需要使用事件委派,就像对
      .js add节
      单击处理程序所做的那样

      $(“.main”).on('change','.js cars项[type=“checkbox”]”,函数(){
      var idx=$(this).closest('li').index();//按顺序获取索引编号
      var chk=$(this).is(“:checked”);//如果选中或不选中,则获取
      var obj=this;//复选框对象
      $(this).closest('.cars').find('.js cars item').each(function(){//Loop every js cars item
      //找到与单击的复选框索引相同的复选框。更改禁用属性
      $(this.find('li:eq('+idx+')[type=“checkbox”]')).not(obj.prop(“checked”,false);
      });
      var checkeds=[];
      $(this).closest(“.cars”).find(“.cars项目输入:复选框:选中”).each(函数(索引){
      checkeds[index]=$(this.attr('id');
      });
      console.clear();
      log(“这些已检查:”,已检查);
      })
      $('.js添加类别')。单击(函数(){
      变量类别内容=`
      
      添加节
      ` $('.main').append(categoryContent); }); $(document.body).on('click','button.js add section',function()){ var sectionContent=`
      • 只需使用
        $(this.parent().append(sectionContent);
        而不是
        $('.cars')。append(sectionContent);

        工作代码

        $('.js cars item[type=“checkbox”]')。更改(函数(){
        var idx=$(this).closest('li').index();//按顺序获取索引编号
        var chk=$(this).is(“:checked”);//如果选中或不选中,则获取
        var obj=this;//复选框对象
        $('.js cars item').each(function(){//循环每个js cars项
        //找到与单击的复选框索引相同的复选框。更改禁用属性
        $(this.find('li:eq('+idx+')[type=“checkbox”]')).not(obj.prop(“checked”,false);
        });
        var checkeds=[];
        $(“.cars项输入:复选框:选中”)。每个(函数(索引){
        checkeds[index]=$(this.attr('id');
        });
        console.clear();
        log(“这些已检查:”,已检查);
        })
        $('.js添加类别')。单击(函数(){
        变量类别内容=`
        
        添加节
        ` $('.main').append(categoryContent); }); $(document.body).on('click','button.js add section',function()){ var sectionContent=`
        • 只需使用
          $(this.parent().append(sectionContent);
          而不是
          $('.cars')。append(sectionContent);

          工作代码

          $('.js cars item[type=“checkbox”]')。更改(函数(){
          var idx=$(this).closest('li').index();//按顺序获取索引编号
          var chk=$(this).is(“:checked”);//如果选中或不选中,则获取
          var obj=this;//复选框对象
          $('.js cars item').each(function(){//循环每个js cars项
          //找到与单击的复选框索引相同的复选框。更改d