jQuery 取得表單資料、單選 Radio 與多選 Checkbox 的方法/判斷 checkbox 是否選取,實現全選跟全部取消
全部選取/取消選取 checkbox
$('#check-all').bind('click',function(){ var data_check = $(this).attr('data-check'); if(data_check === "0"){ $('input[name="place_ids[]"]').prop('checked',true); $('input[name="place_ids[]"]').parent().addClass('checked'); $(this).attr('data-check',1); $(this).html('取消全選'); }else{ $('input[name="place_ids[]"]').prop('checked',false); $('input[name="place_ids[]"]').parent().removeClass('checked'); $(this).attr('data-check',0); $(this).html('全部選取'); } });
轉自 http://blog.miniasp.com/post/2011/04/27/jQuery-get-form-value-Checkbox-Radio.aspx
var cbxVehicle = new Array(); $('input:checkbox:checked[name="vehicle"]').each(function(i) { cbxVehicle[i] = this.value; });
另一個寫法是透過 .map() 將回傳值轉譯成為另一種陣列,如下:
$('input:checkbox:checked[name="vehicle"]').map(function() { return $(this).val(); }).get();
######################################################################
$('input:checkbox:first[name="item_choice[]"]').click(function(){ if($(this).prop("checked")){ $("input:checkbox[name='item_choice[]']").each(function() { $(this).prop("checked", true); }); }else{ $("input:checkbox[name='item_choice[]']").each(function() { $(this).prop("checked", false); }); } });
留言
張貼留言