[英]How to fix dropdown to make one of the hidden div appear and to append
I'm a newbie and I 'm practicing creating a questionnaire that the user can add more question. 我是新手,我正在练习创建问卷,用户可以添加更多问题。 So I have a dropdown with two choices: checkbox and radio button but when I click the checkbox the hidden div does not appear.
所以我有两个选项的下拉列表:复选框和单选按钮但是当我单击复选框时,隐藏的div不会出现。 Also can I use .attr for the choices?
我也可以使用.attr作为选择吗?
<div class="container">
Question:
<br>
<textarea rows = "5" cols = "50" name = "description" placeholder="Enter a question">
</textarea>
<br>
<select name="choice" id="choice" onchange="selectorchecker()">
<option value="">Select choices</option>
<option value="checkbox">Checkbox</option>
<option value="radiobtn">Radio Button</option>
</select>
</div>
Add Question 添加问题
<div style="display:none;" id="chkbox_choice">
<table id="dataTable" width="350px" >
<tr>
<td><input type="checkbox" name="check"/></td>
<td> <INPUT type="text" /> </td>
</tr>
</table>
<input type="button" value="Add choices" onclick="addRow('dataTable')" />
<input type="button" value="Delete choices" onclick="deleteRow('dataTable')" />
</div>
<div style="display:none;" id="rdbtn_choice">
<table id="dataTable" width="350px" >
<tr>
<td><input type="radio" name="radio"/></td>
<td> <INPUT type="text" /> </td>
</tr>
</table>
<input type="button" value="Add choices" onclick="addRow('dataTable')" />
<input type="button" value="Delete choices" onclick="deleteRow('dataTable')" />
</div>
The below script should be changed: 应更改以下脚本:
<select name="choice" id="choice" onchange="selectorchecker()">
...
var elem = document.getElementById("selectorchecker");
to 至
<select name="choice" id="choice">
...
var elem = document.getElementById("choice");
the function getElementById
get the element by its id
, so you should pass the id
to that function 函数
getElementById
通过其id
获取元素,因此您应该将id
传递给该函数
Also can I use .attr for the choices?
我也可以使用.attr作为选择吗?
Yes, you can put the attr to <option>
是的,你可以把attr放到
<option>
<option value="checkbox" custom-field="custom-value">Checkbox</option>
And get it inside the onchange handle: 并在onchange句柄中获取它:
this.options[this.selectedIndex].getAttribute('custom-field')
Another way to do this would be to use the listener that you set on your <select>
tag, and have it call the function you have already initialized rather than set a listener on an item that already has an onchange
event attached to it. 另一种方法是使用你在
<select>
标签上设置的监听器,让它调用你已经初始化的函数,而不是在已经附加了onchange
事件的项目上设置监听器。
function selectorchecker(){
var hiddenDiv = document.getElementById("chkbox_choice");
hiddenDiv.style.display = (this.value == "") ? "none":"block";
}
Also make sure the script is called before the <select>
tag is initialized, so that the html knows the function exists 还要确保在初始化
<select>
标记之前调用脚本,以便html知道该函数存在
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.