简体   繁体   English

选择所有复选框…以及javascript和rails

[英]select all checkboxes… with javascript and rails

I'm running into some type of error here. 我在这里遇到某种类型的错误。 Nothing is happening. 没事 Does anyone have idea idea why? 有人知道为什么吗?

Thank you beforehand! 预先谢谢你!

      -form_tag :action => 'form', :name =>'admin_form' do 
       #images_actions_bar
        .select_all
          =check_box_tag('check_all', 'check_all', false, :onClick => "$$('admin_form input.check').each(function(box){box.checked=true});return false;")

============== ==============

thanks. 谢谢。 what ultimately worked for me was a check box like this: 最终对我有用的是一个复选框,如下所示:

 =check_box_tag('check_all', 'check_all', false, :onClick => "checkAll(document.admin_form.check_all, document.admin_form.checkbox);")

and a js function like this: 和这样的js函数:

function checkAll(field_main, fields){
if(field_main.checked == true){
    for (i = 0; i < fields.length; i++)
        fields[i].checked = true ;
}else{
    for (i = 0; i < fields.length; i++)
        fields[i].checked = false;
}

The answer posted in the original question got me started but I ran into problems because my checkboxes are named message_id[] . 最初问题中发布的答案使我开始工作,但是我遇到了问题,因为我的复选框名为message_id[] The brackets were causing JS syntax errors. 括号导致JS语法错误。

Following a pattern in this thread , I got around this by looping through all the input fields on the form and then operating explicitly on the ones with type="checkbox" . 按照该线程中的模式,我通过在表单上的所有输入字段中循环然后在具有type="checkbox"字段上进行显式操作来解决此问题。

This code in my view creates the Check All checkbox (intentionally without a label): 在我看来,以下代码会创建“检查所有”复选框(故意不带标签):

<%= check_box_tag('check_all', 'check_all', false, :onClick => "checkAll(this);") %>

which uses this JavaScript: 使用以下JavaScript:

function checkAll(check_all){
  // Pass in a named "Check All" checkbox that appears on the same form where all 
  // checkboxes should be checked.

  // Loop through an array containing ALL inputs on same form as check_all
  var inputs = check_all.form.getElementsByTagName("input");
  for (var i = 0; i < inputs.length; i++) {  
    // Only work on checkboxes, and NOT on the "Check All" checkbox
    if (inputs[i].type == "checkbox" && inputs[i].name != check_all.name) { 
      if(check_all.checked == true){
        inputs[i].checked = true ;
      }else{
        inputs[i].checked = false ;
      }
    }  
  }  
}
function checkAll(check_all){
  // Pass in a named "Check All" checkbox that appears on the same form where all 
  // checkboxes should be checked.

  // Loop through an array containing ALL inputs on same form as check_all
  var inputs = check_all.form.getElementsByTagName("input");
  for (var i = 0; i < inputs.length; i++) {  
    // Only work on checkboxes, and NOT on the "Check All" checkbox
    if (inputs[i].type == "checkbox" && inputs[i].name != check_all.name) { 
      inputs[i].checked = check_all.checked ;
    }  
  }  
}

It's not clear what javascript framework you're using. 目前尚不清楚您使用的是哪种JavaScript框架。 I've really only used jQuery and (minimally) Prototype. 我真的只用过jQuery和(至少)原型。 So, I'd check that admin_form is selecting the form element properly. 因此,我将检查admin_form是否正确选择了form元素。 In jQuery, it would have to be form[name="admin_form"] 在jQuery中,它必须是form[name="admin_form"]

I've modified a check/uncheck all jquery piece that I previously wrote. 我已经修改了一个检查/取消选中我先前编写的所有jquery片段。 Hopefully it may help. 希望它会有所帮助。 First, clicking "check_all" will check all checkboxes. 首先,单击“ check_all”将选中所有复选框。 Clicking a checkbox will then uncheck the "check_all" checkbox. 单击复选框,然后取消选中“ check_all”复选框。

$('#check_all').click(function() {
    var checkboxes = $('form[name="admin_form"] > input:checkbox');
    $(checkboxes).each(
        function(){
            this.checked = $('#check_all').attr('checked');
        }
    );
});
$('form[name="admin_form"] > input:checkbox').click(function() {
var checkAll = $('#check_all');
if ($(checkAll).is(':checked') && !($(this).is(':checked')))
     { $(checkAll).removeAttr('checked'); }
 });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM