问题:如何设置radio的checked属性,如何取消radio单选框的选择
解决:使用jquery的prop方法,而不是attr方法,使用jquery的removeAttr方法,而不是removeProp方法
方法:
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>html5,js调用摄像头</title>
<script type="text/javascript" src="jquery-1.12.3.js"></script>
<style>
ul li:hover{
background:#f00;
cursor:pointer;
}
</style>
</head>
<body>
- a
- b
- c
- d
<span>A.</span>
<input id="choiceA" type="radio" value="A" name="choice_ans">
<span>B.</span>
<input id="choiceB" type="radio" value="B" name="choice_ans">
<span>C.</span>
<input id="choiceC" type="radio" value="C" name="choice_ans">
<span>D.</span>
<input id="choiceD" type="radio" value="D" name="choice_ans">
<script>
$(function(){
$('ul li').click(function(){
var val = $(this).text();
var tag = {'a':'A','b':'B','c':'C','d':'D'}[val];
console.log(tag);
$('input[name="choice_ans"]').removeAttr('checked');
$('#choice'+tag).prop('checked','true');
});
});
</script>
</body>
</html>