欢迎各位兄弟 发布技术文章

这里的技术是共享的

You are here

点全选 选中 所有的 checkbox 自己亲自做的 有大用 Is there a way to provide a Select All / Check All checkbox for a field that displays multiple checkboxes in Drupal 7?有大用

shiping1 的头像
$(document).ready(function() {
     var checked = false;
    $('#edit-usersid-all-shipingzhong').click(function() {
        if(!checked){
            $(this).parents('.form-checkboxes').find('input[type="checkbox"]').attr('checked', true);;
            checked = true;
        }else{
            $(this).parents('.form-checkboxes').find('input[type="checkbox"]').attr('checked', false);;
            checked = false;
        }
    });
});

//下面这个好像不行
//Drupal.behaviors.selectAll = {
//    attach: function (context, settings) {
//        $('#edit-usersid-all-shipingzhong').click(function() {
//            alert(1111);
//            $(this).parents('.form-checkboxes').find('input[type="checkbox"]').trigger('click');
//        });
//    }
//};



Seems like there is no core config to setup a "Check All" checkbox unless I missed where it is. I'm creating a content type and have a taxonomy reference field and I have alot of terms, which means alot of boxes I have to check. Anyway to provide a checkbox to select everything?

正确答案

 
1down voteaccepted

Here is an updated version of @duckx code, it has the following improvements :
- only select the checkboxes of the label that has been clicked
- allows to toggle checkboxes

 

Drupal.behaviors.selectAll = {
  attach: function (context, settings) {
    $('body.page-node .form-type-checkboxes > label').click(function() {
        $(this).nextAll('.form-checkboxes').find('input[type="checkbox"]').trigger('click');
    });
  }
};


来自 http://drupal.stackexchange.com/questions/78296/is-there-a-way-to-provide-a-select-all-check-all-che...

 

 
   
which file does Drupal.behaviros.selectAll go? – duckx Sep 27 '13 at 1:02
 
I generally put this in a script.js inside my theme, don't forget to wrap it inside (function ($) { ... })(jQuery); as usual. – tostinni Sep 27 '13 at 6:47

You can do a form alter. hook_form_alter and attach your js to the form.

$form['#attached']['js'] = array(
  drupal_get_path('module', 'YOURMODULE') . '/YOURMODULE.js',
);

Then add the code as in here

shareimprove this answer
 

Here's a quick snippet for anyone looking for a quick check all functionality. It will basically make your field LABEL a check all button. No need to add any extra HTML elements or modules. This just works.

You can pretty much copy this into your js file and it will just work as i restrained it to working only on content Edit pages.

Obviously this isn't a long term solution, but mainly as a quick band aid patch.

jQuery(document).ready(function($) {
  var $checkBoxes = $('input[type="checkbox"]');
  $('body.page-node .form-type-checkboxes > label').click(function() {
      $checkBoxes.attr('checked', true);
  });                                                                                                                                                                                            
});
shareimprove this answer
 

There is no core config option for something like this, it would have to be JavaScript/jQuery (http://jsfiddle.net/PtMrM/).

<a href="#" class="check-all">Check All</a>
<label><input type="checkbox" name="ny"> New York</label>
<label><input type="checkbox" name="bos"> Boston</label>
<label><input type="checkbox" name="sfo"> San Francisco</label>
<label><input type="checkbox" name="dc"> Wasington DC</label>
<script type="text/javascript">
  (function($) {
    var $checkBoxes = $('input[type="checkbox"]');
    $('.check-all').click(function() {
      $checkBoxes.attr('checked', true);
    });
  }(jQuery))
</script>

Please note, that is very quick js, and won't be very user friendly (e.g., they click on it, there is no way to uncheck)

shareimprove this answer
 

This contrib module appears to do the trick: https://www.drupal.org/project/checkall


来自 http://drupal.stackexchange.com/questions/78296/is-there-a-way-to-provide-a-select-all-check-all-che...


Drupal check all checkbox

//add javascript to the page to handle the select all checkbox
drupal_add_js("function toggleSelectAll(cbx){
  if(\$(cbx).attr('checked') == true){
    \$(\$(cbx).parents('form').get(0)).find('input[@type=checkbox]').each(function(){
      if(!this.checked){
        this.checked = true;
      }
    });
  }
  else{
    \$(\$(cbx).parents('form').get(0)).find('input[@type=checkbox]').each(function(){
      if(this.checked){
        this.checked = false;
      }
    });
  }
}
//Only show the select all box if javascript is working
\$(document).ready(function(){\$('#edit-select-all-0').show();});
",'inline');

//add the select all checkbox to the form
$form['select_all'] = array(
    '#type' => 'checkboxes',
    '#options' => array(0 => "Select All"),
    '#attributes' => array('onchange' => "toggleSelectAll(this);", 'style' => 'display : none'),
);

来自 https://snipt.net/asdflars/drupal-check-all-checkbox/
普通分类: