一、獲取單個checkbox選中項(三種寫法)
$("input:checkbox:checked").val()
或者
$("input:[type='checkbox']:checked").val();
或者
$("input:[name='ck']:checked").val();jquery
二、 獲取多個checkbox選中項
$('input:checkbox').each(function() {
if ($(this).attr('checked') ==true) {
alert($(this).val());
}
});數組
三、設置第一個checkbox 爲選中值
$('input:checkbox:first').attr("checked",'checked');
或者
$('input:checkbox').eq(0).attr("checked",'true');dom
四、設置最後一個checkbox爲選中值
$('input:radio:last').attr('checked', 'checked');
或者
$('input:radio:last').attr('checked', 'true');函數
五、根據索引值設置任意一個checkbox爲選中值
$('input:checkbox).eq(索引值).attr('checked', 'true');索引值=0,1,2....
或者
$('input:radio').slice(1,2).attr('checked', 'true');this
六、選中多個checkbox同時選中第1個和第2個的checkbox
$('input:radio').slice(0,2).attr('checked','true');spa
七、根據Value值設置checkbox爲選中值
$("input:checkbox[value='1']").attr('checked','true');.net
八、刪除Value=1的checkbox
$("input:checkbox[value='1']").remove();指針
九、刪除第幾個checkbox
$("input:checkbox").eq(索引值).remove();索引值=0,1,2....
如刪除第3個checkbox:
$("input:checkbox").eq(2).remove();code
十、遍歷checkbox
$('input:checkbox').each(function (index, domEle) {
//寫入代碼
});htm
十一、所有選中
$('input:checkbox').each(function() {
$(this).attr('checked', true);
});
十二、所有取消選擇
$('input:checkbox').each(function () {
$(this).attr('checked',false);
});
1、經過選擇器選取CheckBox:
1.給CheckBox設置一個id屬性,經過id選擇器選取:
1 |
|
JQuery:
1 |
|
2.給CheckBox設置一個class屬性,經過類選擇器選取:
1 |
|
JQuery:
1 2 |
|
3.經過標籤選擇器和屬性選擇器來選取:
1 2 |
|
JQuery:
1 2 |
|
2、對CheckBox的操做:
以這段checkBox代碼爲例:
1 2 3 4 |
|
1.遍歷checkbox用each()方法:
1 |
|
2.設置checkbox被選中用attr();方法:
1 2 |
|
在HTML中,若是一個複選框被選中,對應的標記爲 checked="checked"。 但若是用jquery alert($("#id").attr("checked")) 則會提示您是"true"而不是"checked",因此判斷 if("checked"==$("#id").attr("checked")) 是錯誤的,應該是 if(true == $("#id").attr("checked"))
3.獲取被選中的checkbox的值:
1 2 3 4 |
|
或者:
1 2 3 4 |
|
$("input[name='box']:checked")與 $("input[name='box']")有何區別沒試過,我試了用 $("input[name='box']")能成功。
4.獲取未選中的checkbox的值:
1 2 3 4 5 |
|
5.設置checkbox的value屬性的值:
1 |
|
在jquery中,遍歷對象和數組,常常會用到$().each和$.each(),兩個方法。兩個方法是有區別的,從而這兩個方法在針對不一樣的操做上,顯示了各自的特色。
$().each,對於這個方法,在dom處理上面用的較多。若是頁面有多個input標籤類型爲checkbox,對於這時用$().each來處理多個checkbook,例如:
$(「input[name=’ch’]」).each(function(i){
if($(this).attr(‘checked’)==true)
{
//一些操做代碼
}
回調函數是能夠傳遞參數,i就爲遍歷的索引。
對於遍歷一個數組,用$.each()來處理,簡直爽到了極點。例如:
$.each([{「name」:」limeng」,」email」:」xfjylimeng」},{「name」:」hehe」,」email」:」xfjylimeng」},function(i,n)
{
alert(「索引:」+i,」對應值爲:」+n.name);
});
參數i爲遍歷索引值,n爲當前的遍歷對象.
var arr1 = [ 「one」, 「two」, 「three」, 「four」, 「five」 ];
$.each(arr1, function(){
alert(this);
});
輸出:one two three four five
var arr2 = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
$.each(arr2, function(i, item){
alert(item[0]);
});
輸出:1 4 7
var obj = { one:1, two:2, three:3, four:4, five:5 };
$.each(obj, function(key, val) {
alert(obj[key]);
});
輸出:1 2 3 4 5
在jQuery裏有一個each方法,用起來很是的爽,不用再像原來那樣寫for循環,jQuery源碼裏本身也有不少用到each方法。
其實jQuery裏的each方法是經過js裏的call方法來實現的。
下面簡單介紹一下call方法。
call這個方法很奇妙,其實官方的說明是:「調用一個對象的一個方法,以另外一個對象替換當前對象。」網上更多的解釋是變換上下文環境,也有說是改變上下文this指針。
call([thisObj[,arg1[, arg2[, [,.argN]]]]])
參數
thisObj
可選項。將被用做當前對象的對象。
arg1, arg2, , argN
可選項。將被傳遞方法參數序列。
說明
call 方法能夠用來代替另外一個對象調用一個方法。call 方法可將一個函數的對象上下文從初始的上下文改變爲由 thisObj 指定的新對象。
引用網上有一個很經典的例子
Js代碼
function add(a,b)
{
alert(a+b);
}
function sub(a,b)
{
alert(a-b);
}
add.call(sub,3,1);
用 add 來替換 sub,add.call(sub,3,1) == add(3,1) ,因此運行結果爲:alert(4);
注意:js 中的函數實際上是對象,函數名是對 Function 對象的引用。
具體call更深刻的就不在這裏提了。
下面提一下jQuery的each方法的幾種經常使用的用法
Js代碼
var arr = [ 「one」, 「two」, 「three」, 「four」];
$.each(arr, function(){
alert(this);
});
//上面這個each輸出的結果分別爲:one,two,three,four
var arr1 = [[1, 4, 3], [4, 6, 6], [7, 20, 9]]
$.each(arr1, function(i, item){
alert(item[0]);
});
//其實arr1爲一個二維數組,item至關於取每個一維數組,
//item[0]相對於取每個一維數組裏的第一個值
//因此上面這個each輸出分別爲:1 4 7
var obj = { one:1, two:2, three:3, four:4}; $.each(obj, function(key, val) { alert(obj[key]); }); //這個each就有更厲害了,能循環每個屬性 //輸出結果爲:1 2 3 4