使用jQuery获取checkbox的已选项
代码
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>使用jQuery获取checkbox的已选项</title>
<script type="text/javascript" src="jquery-1.4.2.min.js"></script>
<script type="text/javascript">
function getChecked() {
var chkID = "";
$("#div_Container input[type='checkbox']:checked").each(function() {
chkID += this.id + ",";
});
if (chkID != "") {
chkID = chkID.substring(0, chkID.length - 1);
chkID = chkID.replace(/chk_/g, "");
}
alert(chkID);
}
</script>
</head>
<body>
<div id="div_Container">
<input type="checkbox" id="chk_1" />chk_1
<input type="checkbox" id="chk_2" />chk_2
<input type="checkbox" id="chk_3" />chk_3
<input type="checkbox" id="chk_4" />chk_4
</div>
<input type="button" value="显示结果" onclick="getChecked()" />
</body>
</html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>使用jQuery获取checkbox的已选项</title>
<script type="text/javascript" src="jquery-1.4.2.min.js"></script>
<script type="text/javascript">
function getChecked() {
var chkID = "";
$("#div_Container input[type='checkbox']:checked").each(function() {
chkID += this.id + ",";
});
if (chkID != "") {
chkID = chkID.substring(0, chkID.length - 1);
chkID = chkID.replace(/chk_/g, "");
}
alert(chkID);
}
</script>
</head>
<body>
<div id="div_Container">
<input type="checkbox" id="chk_1" />chk_1
<input type="checkbox" id="chk_2" />chk_2
<input type="checkbox" id="chk_3" />chk_3
<input type="checkbox" id="chk_4" />chk_4
</div>
<input type="button" value="显示结果" onclick="getChecked()" />
</body>
</html>
关键是:$("#div_Container input[type='checkbox']:checked")


浙公网安备 33010602011771号