js获取复选框值

时间:2024-01-13 22:30:08
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>本地首页</title>
</head>
<body>
<p>爱好特长:</p>
JAVA<input type="checkbox" name="IT" value="JAVA" >
.Net<input type="checkbox" name="IT" value=".Net" >
PHP<input type="checkbox" name="IT" value="PHP" >
<hr>
<input type="button" value="确定" id="sure">
</body>
<script src="jquery-3.2.1.min.js"></script>
<script>
$('#sure').click(function () {
var obj = $("input[name='IT']");
var s='';
for(j=0;j<obj.length;j++){
if(obj[j].checked){
s +=obj[j].value;
} }
alert(s);
return false;
for(var i=0;i<obj.length;i++){
if(obj[i].checked)
s +=obj[i].value+',';
}
alert(s);
})
</script>
</html>