<html><head> <script src="jquery-1.11.1.min.js" type="text/javascript"></script></head><body> <input type="checkbox" name="chk_list[]" value="1" />1 <input type="checkbox" name="chk_list[]" value="2" />2 <input type="checkbox" name="chk_list[]" value="3" />3 <input type="checkbox" name="chk_list[]" value="4" />4 <input type="checkbox" name="chk_all" id="chk_all" />全選/取消全選<script type="text/Javascript"> $("#chk_all").click(function(){ // 使用attr只能執行一次 $("input[name='chk_list[]']").attr("checked", $(this).attr("checked")); // 使用PRop則完美實現全選和反選 $("input[name='chk_list[]']").prop("checked", $(this).prop("checked"));
// 獲取所有選中的項并把選中項的文本組成一個字符串
var str = '';
$($("input[name='chk_list[]']:checked")).each(function(){
str += $(this).next().text() + ',';
});
alert(str); });</script></body></html>
總結:
新聞熱點
疑難解答