標籤:cli click 對象 實現 res 多個 efi type cti
<td width="82%" colspan="3">
<input type="checkbox" id="all">全選
<input type="checkbox" id="reverse">反選
</td>
<td width="82%" colspan="3">
<s:checkboxlist name="resUuids" list="resList" listKey="uuid" listValue="name"></s:checkboxlist>
</td>
$(function(){
//全選
$("#all").click(function(){
//將下面所有組件全部選中
//$("[name=resUuids]") 是多個組件,整體是個對象數組
//$("[name=resUuids]").attr("checked","checked");
//先擷取當前組件的狀態
//$(this).attr("checked")
//將所有組件設定為對應狀態
//$("[name=resUuids]").attr("checked",$(this).attr("checked"));
//$(this).attr("checked")擷取的值究竟是什麼
//alert($(this).attr("checked")); //undefined
//$("[name=resUuids]").attr("checked","undefined");
//js文法規則,除了false,FALSE,"false","FALSE",0五個值之外的所有值,認定為true
//$("[name=resUuids]").attr("checked",false);
var flag = $(this).attr("checked");
$("[name=resUuids]").attr("checked",flag == "checked");
});
//反選
$("#reverse").click(function(){
//將所有組件的狀態切換成原始狀態的反狀態
//$("[name=resUuids]").attr("checked",!($("[name=resUuids]").attr("checked")=="checked"));
//當選取器選中的組件是多個時,擷取組件的任何資料都是對第一個組件進行操作
//alert(!($("[name=resUuids]").attr("checked")=="checked"));
//對每個組件進行迭代,讓其操作狀態為對應組件的原始狀態的反狀態
$("[name=resUuids]").each(function(){
//使用each操作實現對每個組件的操作
var flag = $(this).attr("checked");
$(this).attr("checked", !(flag =="checked"));
});
checkSelect();
});
//綁定組件
$("[name=resUuids]").click(function(){
//將全選的狀態設定為基於所有組件的綜合狀態值
checkSelect();
});
function checkSelect(){
var allFlag = true;
$("[name=resUuids]").each(function(){
var flag = $(this).attr("checked") == "checked";
//&:位元運算與 &&:邏輯與
allFlag = allFlag && flag;
});
$("#all").attr("checked",allFlag);
}
});
jquery 全選 全不選 事件綁定