【问题标题】:Checkbox table to select rows用于选择行的复选框表
【发布时间】:2013-07-23 08:46:33
【问题描述】:

您好,我刚刚创建了一个包含一些值的表。我想用第一个复选框选择所有复选框并取消选中它们。 print "Content-type: text/html; charset=iso-8859-1\n\n";

my $script = qq{
\$('#id').change(function() {
var checkboxes = \$(this).closest('form').find(':checkbox');
if(\$(this).is(':checked')) {
    checkboxes.attr('checked', 'checked');
} else {
    checkboxes.removeAttr('checked');
}

}); };

use CGI::Carp qw(fatalsToBrowser);

$q = new CGI;
print $q->start_html(
-title=>"Read a File",
-style  => {-src =>'css/ui-lightness/jquery-ui-1.10.3.custom.css" rel="stylesheet'},
     -script => [
        {-src =>'js/jquery-1.9.1.js'}, 
        {-src =>'js/jquery-ui-1.10.3.custom.js'

        }, 
      ],   
);

my @aRows = ();

my $oCheckAll = $q->input({-type => 'checkbox', -name => 'sel_all', -id =>'id' }, 'Select All');

# Add header of table
push @aRows, (
   $q->Tr($q->th([$oCheckAll, 'Name', 'Surname', 'DB'])),   
);

# Add table rows
for(my $i=0; $i<4; $i++) {
   push @aRows, (
      $q->Tr($q->td([$q->input({-type => 'checkbox' -id => 'id'}), "EMR", "MB", $i]))
   );
}

print $q->style(".table th, td { width: 25%;}");

print $q->tabl

e({-class=> '表', -b

【问题讨论】:

  • 你已经用javascript标签标记了这个,JS代码在哪里?
  • 顺便问一下这是什么语言:)
  • 你能更新我编辑的代码吗?
  • 请创建一个 jsfiddle 示例,您的代码非常混乱,并且不清楚您使用的语言。 (其中一个和哪里)另外,用正确的重新标记。

标签: javascript html


【解决方案1】:
$('input:checkbox').change(function (){
    var thisCheck = $(this);
    if (thischeck.is(':checked')){
        thisCheck.parent().children('input:checkbox').attr('checked','checked');
    }
});

希望对你有帮助!!!

【讨论】:

  • 当我选中第一个复选框时,没有其他复选框被选中。我正在使用这个 \$('#id').change(function() { var checkboxes = $(this).closest('form').find(':checkbox'); if($(this).is( ':checked')) { checkboxes.attr('checked', 'checked'); } else { checkboxes.removeAttr('checked'); } });但即使这样也无济于事
  • 你能在js fiddle上创建一个例子吗?我会在那里更新你的代码。
  • 刚刚编辑了我的代码,看看 pelase 。我也想要取消选中选项
【解决方案2】:

我认为这是这个变体中的一个错误:

$('table tr td:first-child input').click(function(event){
  $(this).parent().parent().find("td input").attr("checked", $(this).is(":checked"));
});  

请参阅this 链接。

$('table tr td:first-child input').click(function(event){
  if ($(this).is(':checked')) {
    $(this).parent().parent().find("td input:not(:checked)").click();
  } else {
    $(this).parent().parent().find("td input:checked").click();
  }
});

【讨论】:

  • 我不工作“未捕获的语法错误:输入意外结束”
  • @Armida 在什么浏览器中?我在 IE、Opera、Firefox、Chrome 和 Safari 中有很好的工作。你真的看过这个链接吗? http://jsfiddle.net/ostapische/7shEz/1/
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-11-17
  • 2019-12-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多