2014-01-30 146 views
1

我有一个以动态方式生成/克隆复选框的jquery函数。我可以添加或删除尽可能多的复选框。我在做两件事情时遇到了一些困难:当检查main_item复选框时,如何显示sub_item复选框。这里是一个DEMO动态创建多个复选框

jQuery的

$('#btnAdd').click(function() { 
    var num = $('.clonedSection').length; 
    var newNum = new Number(num + 1); 

    var newSection = $('#pq_entry_' + num).clone().attr('id', 'pq_entry_' + newNum); 
    newSection.find('input[type="text"]').val(''); 
    newSection.find('input[type="checkbox"]').prop('checked', false); 
    newSection.children(':first').children(':first').attr('id', 'main_item_' + newNum).attr('name', 'main_item_' + newNum).attr('placeholder', 'Item #' + newNum + ' Name'); 
    newSection.children(':nth-child(2)').children(':first').attr('id', 'sub_item_' + newNum).attr('name', 'sub_item_' + newNum); 
    newSection.children(':nth-child(3)').children(':first').attr('id', 'other_item_' + newNum).attr('name', 'other_item_' + newNum); 
    newSection.insertAfter('#pq_entry_' + num).last(); 

    $('#btnDel').prop('disabled', ''); 

    if (newNum == 10) $('#btnAdd').prop('disabled', 'disabled'); 
    }); 

    $('#btnDel').click(function() { 
    var num = $('.clonedSection').length; // how many "duplicatable" input fields we currently have 
    $('#pq_entry_' + num).remove(); // remove the last element 

    // enable the "add" button 
    $('#btnAdd').prop('disabled', ''); 

    // if only one element remains, disable the "remove" button 
    if (num - 1 == 1) $('#btnDel').prop('disabled', 'disabled'); 
    }); 

    $('#btnDel').prop('disabled', 'disabled'); 
    }); 

HTML

 <div> 
     <ul id="pq_entry_1" class="clonedSection"> 
     <li style="list-style-type: none;"> 
     <input id="main_item_1" name="main_item_1" type="checkbox"><label>Main Item</label> 
     </li> 
      <li style="list-style-type: none;"> 
       <input id="sub_item_1" name="sub_item_1" type="checkbox"><label>Sub Item</label> 
      </li> 
     <li style="list-style-type: none;"> 
      <input id="other_item_1" name="other_item_1" type="checkbox"><label>Other Item</label> 
     </li> 
     </ul> 
     <center><input type='button' class="button tiny radius" id='btnAdd' value='Add Another' /> 
     <input type='button' class="button tiny radius alert" id='btnDel' value='Delete Last' /></center> 
     </div> 

显示效果

enter image description here

+0

你所说的这个意思呢'当main_item复选框被选中时如何显示sub_item复选框。' –

+0

@FurquanKhan对不起,我说得更清楚了。再次检查我的帖子以获取更多详情。 – techAddict82

回答

1

如果我没理解好了,这样的事情:

$('#main-panel').on('click', '.main-item', function() { 
    var $mainItem = $(this); 
    var $subItem = $mainItem.parent().next('.sub-item'); 
    if ($mainItem.is(':checked')) { 
     $subItem.show(); 
    } else { 
     $subItem.hide(); 
    } 
}); 

与此标记:

<div id="main-panel"> 
    <ul id="pq_entry_1" class="clonedSection"> 
     <li> 
      <input id="main_item_1" class='main-item' name="main_item_1" type="checkbox" /> 
      <label>Main Item</label> 
     </li> 
     <ul class="sub-item" style='display: none;'> 
      <li> 
       <input id="sub_item_1" name="sub_item_1" type="checkbox" /> 
       <label>Sub Item</label> 
      </li> 
     </ul> 
     <!-- same as yours --> 

现场演示:jsFiddle

+0

有没有办法当我点击按钮'添加另一个'来隐藏下一组子项复选框? – techAddict82

+0

而且我怎样才能做到以下几点:如果'main_item'没有被选中,那么取消选中'sub_items'? – techAddict82

0

的主要问题是,你的子项是不是真的在此刻子项目。将子项目放在它们的子项目中,然后生成简单的脚本。像这样

HTML:

<div id="items"> 
    <ul id="pq_entry_1" class="mainitems"> 
     <li style="list-style-type: none;"> 
      <input id="main_item_1" name="main_item_1" type="checkbox" class="mainitem"> 
      <label>Main Item</label> 
      <ul class="subitems"> 
       <li style="list-style-type: none;"> 
        <input id="sub_item_1" name="sub_item_1" type="checkbox" class="subitem"> 
        <label>Sub Item</label> 
       </li> 
      </ul> 
     </li> 
     <li style="list-style-type: none;"> 
      <input id="other_item_1" name="other_item_1" type="checkbox" class="mainitem"> 
      <label>Other Item</label> 
     </li> 
    </ul> 
</div> 

的CSS:

.subitems 
{ 
    display: none; 
} 

的Javascript(jQuery的)的一部分,你还不具备:

$(function() { 
    $("#items").on("click", ".mainitem", function() { 
     $(this).closest("ul").find(".subitems")[$(this).prop("checked") ? "fadeIn": "fadeOut"](); 
    }); 
});