欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Jquery遍歷checkbox獲取選中項(xiàng)value值的方法

 更新時(shí)間:2014年02月13日 16:46:39   作者:  
這篇文章主要介紹了Jquery遍歷checkbox獲取選中項(xiàng)的value值,需要的朋友可以參考下
源碼:
復(fù)制代碼 代碼如下:

jQuery(function($){
$("input[name='key']:checkbox").click(function(){
var ids = '';
var flag = 0;
$("#ids").attr("value",ids);
$("input[name='key']:checkbox").each(function(){
if (true == $(this).attr("checked")) {
ids += $(this).attr('value')+',';
flag += 1;
}
});
if(0 < flag) {
$("#ids").attr("value",ids);
return true;
}else {
alert('請至少選擇一項(xiàng)!');
return false;
}
});
});

本源碼的功能:

獲取name=‘key'的復(fù)選框的值,將選中項(xiàng)的 value 寫到隱藏域 <input type="hidden" name="ids" id="ids" value="" /> 的表單中。

核心語句:
復(fù)制代碼 代碼如下:

$("input[name='key']:checkbox").each(function(){
if (true == $(this).attr("checked")) {
ids += $(this).attr('value')+',';
}
});

在HTML中,如果一個(gè)復(fù)選框被選中,對(duì)應(yīng)的標(biāo)記為 checked="checked"。 但如果用jquery alert($("#id").attr("checked")) 則會(huì)提示您是"true"而不是"checked",所以判斷 if("checked"==$("#id").attr("checked")) 是錯(cuò)誤的,應(yīng)該如上面那樣書寫: if(true == $("#id").attr("checked"))

相關(guān)文章

最新評(píng)論