我正在动态生成一些这样的单选按钮:
degree_info.innerHTML += '<label for="radio">Current Work:</label>\
<input type="radio" name="work" value = "project" id="project" onclick = "addMembers(1);" />Project\
<input type="radio" name="work" value = "thesis" id="thesis" onclick = "addMembers(2);" />Thesis <br />';
on click事件正常,但是当我点击一个单选按钮时,单选按钮不会将checked
设置为true
,而是保持灰色。
更新:
从true
返回addMembers()
无法解决问题
更新:
发布给JSFiddle:http://jsfiddle.net/ELjDg/2/
此处还有我的addMembers()
方法
function addMembers(num){
committee_members = document.getElementById("committee_members");
if(committee_members){
document.getElementById("degree_info").removeChild(committee_members);
}
degree_info = document.getElementById("degree_info");
degree_info.innerHTML += "<div id='committee_members'></div>";
for(i = 0; i < num; i++){
degree_info.lastChild.innerHTML += '<label for="member">Committee Member:</label>\
<input type="text" name="member' + i + '" />';
}
return true;
}
答案 0 :(得分:1)
您需要实际将复选框属性checked
设置为true
,而不仅仅是return true
。在addMembers(num)
中添加以下代码:
if(num == 1) document.getElementById("project").checked = true;
else if(num == 2) document.getElementById("thesis").checked = true;
希望这有帮助。