2016-08-01 78 views
-1

我在HTML表单中有以下复选框。通过名称选择复选框的按钮

<ul id="selection"> 
    <span><label>First<br><input type="checkbox" name="First"></label></span> 
    <span><label>Second<br><input type="checkbox" name="Second"></label></span> 
    <span><label>Third<br><input type="checkbox" name="Third"></label></span> 
    <span><label>Fourth<br><input type="checkbox" name="Fourth"></label></span> 
    <span><label>Fifth<br><input type="checkbox" name="Fifth"></label></span> 
</ul> 

我试图创建两个按钮。一个会选择前两个,一个选择全部。
我设法使用这一个按钮,选择所有的人:

<input type="button" class="check btn" onClick="select(this)" value="Select All"/> 
    <script> 
     $('.check:button').click(function(){ 
      var checked = !$(this).data('checked'); 
      $('input:checkbox').prop('checked', checked); 
      $(this).val(checked ? 'uncheck all' : 'Select') 
      $(this).data('checked', checked); 
     }); 
    </script> 

但我似乎无法得到这两个按钮的工作。
我的JavaScript知识是相当有限的,一直在寻找几天,但没有成功的解决方案。
可以请别人帮忙吗?详细的解释会很棒。

+0

HTTP: //stackoverflow.com/questions/1107220/how-can-i-select-an-elem ENT按姓名与 - jQuery的 –

回答

1

试试这个:

$("#selectall").click(function() { 
    $("input[type='checkbox']").prop('checked', true); 
}); 
$("#selecttwo").click(function() { 
    $("input[name='First'], input[name='Second']").prop('checked', true); 
}); 
$("#resetall").click(function() { 
    $("input[type='checkbox']").prop('checked', false); 
}); 

DEMO