2014-02-05 48 views
1

我一直試圖根據以下鏈接爲表單創建「全​​選」複選框,但我不斷收到錯誤。我搜查了,但沒有運氣搞清楚這一點。我不確定是否有選擇器正確設置。任何幫助將非常感激!選中所有帶有文本鏈接的複選框:錯誤

Select all checkboxes with a text link

形式

<li id='field_1_17' class='gfield gfield_html gfield_html_formatted gfield_no_follows_desc' > 
    <a href="#' id="checkall">Select all checkboxes</a> 
</li> 

<li id='field_1_12' class='gfield' > 
    <label class='gfield_label'>Wednesday 1/22</label> 
    <div class='ginput_container'> 
     <ul class='gfield_checkbox' id='input_1_12'> 
      <li class='gchoice_12_1'> 
       <input name='input_12.1' type='checkbox' value='2:30-2:40 Welcome and introduction' id='choice_12_1' tabindex='11' /> 
       <label for='choice_12_1'>2:30-2:40 Welcome and introduction</label></li> 

腳本從其他崗位

<script type="text/javascript"> 
    $(function() { 
     $('#checkall').click(function() { 
      var checked = $(this).data('checked'); 
      $('input_1_12').find(':checkbox').attr('checked', !checked); 
      $(this).data('checked', !checked); 
     }); 
    }); 
</script> 
+0

你正在得到什麼錯誤? – talemyn

+0

沒有這個代碼的錯誤,只是沒有得到檢查 – Sam1600

回答

1

使用你漏點。

$('input_1_12').find(':checkbox').prop('checked', !checked); 

應該是:

$('.input_1_12').find(':checkbox').prop('checked', !checked); 

甚至更​​好:

$('.input_1_12[input[type="checkbox"]').prop('checked', !checked); 

更新時間: 改變attr()prop()因爲ATTR方法需要一個字符串。

更新2:這裏是你可以使用attr

var checked = $(this).data('checked'), 
    checkboxes = $('.input_1_12[input[type="checkbox"]'); 
if (checked) { 
    checkboxes.removeAttr("checked"); 
}else { 
    checkboxes.attr("checked","checked"); 
} 
+1

建議'.prop()' – Satpal

+0

第二個建議'.prop()' –

+0

感謝您的迴應!我試圖添加,但仍然沒有得到「檢查」我看到網址後附加「/#'id =」沒有錯誤顯示在控制檯 – Sam1600

0

一個ID選擇與#開始做什麼,你應該用prop()設置檢查財產

$(function() { 
    $('#checkall').on('click', function() { 
     var checked = $(this).data('checked'); 
     $('#input_1_12 [type="checkbox"]').prop('checked', !checked); 
     $(this).data('checked', !checked); 
    }); 
}); 
+0

謝謝腺!不幸的是,我也嘗試過這一點,但沒有運氣 - 正如我上面提到的 - 「沒有得到'檢查'我看到URL被追加」/#'id =「控制檯中沒有錯誤顯示」 – Sam1600