<body>
<input type='checkbox' name='likes' value='music'></input><span>音乐</span>
<input type='checkbox' name='likes'></input><span>美术</span>
<input type='checkbox' name='likes'></input><span>信息技术</span>
<input type='checkbox' name='likes'></input><span>运动</span>
<input type='radio' name='other'><span>单选</span></input>
<script src="js/jquery-1.6.4.min.js"></script>
<script>
var hobbies;
$("input[name=likes]").change(function(){
hobbies='';
//console.log($(this)[0].checked);
$("input:checked[name=likes]").each(function(index){
console.log($(this).next('span'));
if($(this)[0].checked){
hobbies+=$(this).next('span').text();
hobbies+=",";
}
});
console.log(hobbies.slice(0,-1));
});
</script>
<input type='checkbox' name='likes' value='music'></input><span>音乐</span>
<input type='checkbox' name='likes'></input><span>美术</span>
<input type='checkbox' name='likes'></input><span>信息技术</span>
<input type='checkbox' name='likes'></input><span>运动</span>
<input type='radio' name='other'><span>单选</span></input>
<script src="js/jquery-1.6.4.min.js"></script>
<script>
var hobbies;
$("input[name=likes]").change(function(){
hobbies='';
//console.log($(this)[0].checked);
$("input:checked[name=likes]").each(function(index){
console.log($(this).next('span'));
if($(this)[0].checked){
hobbies+=$(this).next('span').text();
hobbies+=",";
}
});
console.log(hobbies.slice(0,-1));
});
</script>
若把放里,用$.fn.children('span')(为空)并不能获得span。
ps:用jquery可以获得checkbox的value属性的值(默认为on, val()),其text(),htm()获得的值都是空字符串。