jQuery中取得checkbox選取項等操作及注意事項_jquery
May 16, 2016 pm 05:12 PM1. 取得checkbox的選取項
2. checkbox選項的全選反選操作
測試的checkbox程式碼片段:
代碼如下:
程式碼如下:
$("input[name='check,box'box'boxs. checked]").each(function () {
alert(this.value);
})
但在測試時我就遇到了問題,這種方法在IE下可以獲取,但在firefox和chrome瀏覽器下就無法取得目前的選取項,測試效果如下:
IE下的測試效果(我的是IE10):
IE10下的效果:
chrome瀏覽器下的效果:
Jquery 選取幾個input CheckBox問題,IE正常,FF和Chrome無法取到值
複製程式碼
程式碼如下:
///取得選取項huoqu2').click(function () {
$('#show').html(""); //alert(this.value);
在chrome下的效果:
二:checkbox的全選 反選操作:
由於這兩個比較簡單,所以我就直接上程式碼吧:
//全選/取消全選
//全選/取消全選
$("input[name='abc']").attr("checked, 'true');
$("input[name='abc']").removeAttr("checked");
//反選
input[name='abc']").each(function () {
Attr("checked");
} else {
", 'true');
}
🎜>
jquery版本在1.3之前時,取得checkbox的選取項目的操作:
jquery版本在1.3之後時,取得checkbox的選取項目的操作:
複製碼
程式碼如下:
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
<script src="js/jquery-1.7.2.min.js"></script>
<script>
$(function () {
//获取选中项(FF和chrome下无效)
$('#huoqu').click(function () {
//$("input[name='abc'][checked]").each(function () {
// alert(this.value);
//});
$('#show').html("");
$("input[name='abc'][checked]").each(function () {
//alert(this.value);
$('#show').append(this.value + " ");
});
});
//获取选中项
$('#huoqu2').click(function () {
$('#show').html("");
$("input[name='abc']:checked").each(function () {
//alert(this.value);
$('#show').append(this.value + " ");
});
});
//全选/取消全选
$('#quanxuan').toggle(function () {
$("input[name='abc']").attr("checked", 'true');
}, function () {
$("input[name='abc']").removeAttr("checked");
});
//反选
$('#fanxuan').click(function () {
$("input[name='abc']").each(function () {
if ($(this).attr("checked")) {
$(this).removeAttr("checked");
} else {
$(this).attr("checked", 'true');
}
});
});
});

熱門文章

熱門文章

熱門文章標籤

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)