这是语法或逻辑中的问题(不可能与按钮同时创建文本)?谢谢您的帮助.
在我的HTML正文中,我有这个:
<form ID="form1"><input type="radio" ID= "form1_no" value="no" checked> <label for = "form1_no" >No</label><input type="radio" ID= "form1_yes" value="yes" onClick= exam()> <label for = "form2_yes" >Yes</label></form>
功能考试()是:
<script type='application/JavaScript'>function exam() { var inputno = document.createElement("input"); inputno.type = "radio"; inputno.ID = "form2_no"; inputno.value = "no"; inputno.onclick = function () {alert("I select No in Form 2")}; document.getElementByID("form2").appendChild(inputno); // this is working var inputyes = document.createElement("input"); inputyes.type = "radio"; inputyes.ID = "form2_yes"; inputyes.value ="yes"; inputyes.onclick = function () {alert("I select Yes in Form 2")}; document.getElementByID("form2").appendChild(inputyes); // this is working // Now,the code that is not working: // 1st tentative (adding "Yes" and "No" as plain text after their radio button): var textno = "No"; document.getElementByID("form2_no").appendChild(textno); var textyes = "Yes"; document.getElementByID("form2_yes").appendChild(textyes); // 2nd tentative (adding "Yes" and "No" as labels to their radio button): var labelno = document.createElement("label"); labelno.for="form2_no"; labelno.INNERHTML = "No"; document.getElementByID("form2_no").appendChild(labelno); var labelyes = document.createElement("label"); labelyes.for="form2_yes"; labelyes.INNERHTML = "Yes"; document.getElementByID("form2_yes").appendChild(labelyes);} </script>解决方法 这样的东西可用于创建按钮和标签.
<div ID="radio_home"></div><script> var radio_home = document.getElementByID("radio_home"); function makeRadiobutton(name,value,text) { var label = document.createElement("label"); var radio = document.createElement("input"); radio.type = "radio"; radio.name = name; radio.value = value; label.appendChild(radio); label.appendChild(document.createTextNode(text)); return label; } var yes_button = makeRadiobutton("yesbutton","yes","Oh yea! do it!"); radio_home.appendChild(yes_button);</script>总结
以上是内存溢出为你收集整理的在HTML中,使用Javascript,创建新的单选按钮及其文本?全部内容,希望文章能够帮你解决在HTML中,使用Javascript,创建新的单选按钮及其文本?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)