2013-07-23 100 views
0

嗨,我剛剛創建了一些表的一些值。我想用第一個複選框選中所有複選框,並取消選中它們。 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({ - 類=> '表',-b

+3

你已經用'javascript'標籤標記了這個,JS代碼在哪裏? – Teemu

+2

這是什麼語言的方式:) – coolguy

+0

可以更新我編輯的代碼嗎? – Armida

回答

0
$('input:checkbox').change(function(){ 
    var thisCheck = $(this); 
    if (thischeck.is(':checked')){ 
     thisCheck.parent().children('input:checkbox').attr('checked','checked'); 
    } 
}); 

希望這有助於!

+0

當我檢查第一個複選框沒有其他複選框被選中,因爲我想要的。使用這個\ $('#id').import(function(){ var checkboxes = $(this).closest('form')。find(':checkbox'); if($(this)。 (':checked')){ checkboxes.attr('checked','checked'); } else { checkboxes.removeAttr('checked'); } });但即使這不幫助 – Armida

+0

你可以在js小提琴上創建一個例子。我會在那裏更新你的代碼。 – mechanicals

+0

只是編輯我的代碼,看看pelase。我也想要取消選項 – Armida

0

我覺得這裏是在這個變體中的錯誤:

$('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(); 
    } 
}); 
+0

我不工作「Uncaught SyntaxError:意外的輸入結束」 – Armida

+0

@Armida在什麼瀏覽器?我在IE,Opera,Firefox,Chrome和Safari中都有很好的工作。你真的看過這個鏈接? [http://jsfiddle.net/ostapische/7shEz/1/](http://jsfiddle.net/ostapische/7shEz/1/) – ostapische