[英]Hiding/displaying Divs onChange
I have a variety of divs with form fields. 我有各种带有表单域的div。 Based on a drop-down list, i need it to display the div corresponding to that value, and hide all the other divs. 基于一个下拉列表,我需要它显示对应于该值的div,并隐藏所有其他div。 (Perhaps there's a more efficient way to even do that, rather than load all the divs and just hide them, i'm not sure). (我不确定,也许有一种更有效的方法甚至可以做到这一点,而不是加载所有的div并将它们隐藏起来)。
What I have right now is this: 我现在所拥有的是:
<select id="group" name="group">
<option></option>
<option value="8">Testing This Stuff</option>
<option value="9">Testing Other Stuff</option>
</select>
<div id="8" style="display:none;">**form fields**</div>
<div id="9" style="display:none;">**different form fields**</div>
And the current, semi-working javascript: 和当前的半工作javascript:
<script type="text/javascript">
window.onload = function() {
var eSelect = document.getElementById('group');
eSelect.onchange = function() {
var id = eSelect.selectedIndex;
document.getElementById(id).style.display = 'block';
}
}
</script>
What i'm getting is 'id' is null, so i haven't been able to work past that part yet. 我得到的是'id'为null,所以我还无法通过那部分工作。
Part 2 would be hiding the old div and displaying the new div (if they were to change the option selected). 第2部分将隐藏旧的div并显示新的div(如果他们要更改选择的选项)。 Any suggestions? 有什么建议么?
Solution: 解:
<script type="text/javascript">
window.onload = function() {
var current;
var eSelect = document.getElementById('group');
eSelect.onchange = function() {
var id = eSelect.value;
if (current && current != id) {
document.getElementById(current).style.display = 'none'
}
document.getElementById(id).style.display = 'block';
current = id;
}
}
</script>
<select id="materialsgroup" class="formInput" name="materialsgroup">
<option value=""></option>
<option value="groupid_8">Testing This Stuff</option>
<option value="groupid_9">Testing Other Stuff</option>
</select>
<div id="groupid_8">**form fields**</div>
<div id="groupid_9">**more form fields**</div>
To make the select and divs, this is what I've done: 为了进行选择和div,这是我要做的:
foreach($groups as $key=>$value)
{
$valueItem = "groupid_".$value['group_id'];
$text = $value['title'];
$htmlString .= sprintf('<option value="%s">%s</option>', $valueItem, $text);
}
echo '<tr>
<td class="formLabelCell">Group</td>
<td class="formInputCell">
<select id="group" class="formInput" name="group">'.$htmlString.'</select></td></tr>';
foreach($groups as $gkey=>$gvalue)
{
echo '<div id=groupid_'.$groups[$gkey]['group_id'].' style="display:none;">';
//**Draw the form stuff here**
echo '</div>';
}
Try using 尝试使用
var id = eSelect.value;
For the second part, if you are using pure javascript (I mean, you're not using a javascript framework like jQuery) maybe a good solution is to load an array containing the div's ids and iterate it, hiding the div != eSelect.value 对于第二部分,如果您使用的是纯javascript(我的意思是,您没有使用jQuery这样的javascript框架),也许一个好的解决方案是加载包含div的ID的数组并对其进行迭代,隐藏div!= eSelect。值
var arrDivs = new Array("1","2","3");
for (var i=0; i < arrDivs.length; i++) {
if (arrDivs[i] == eSelect.value)
document.getElementById(arrDivs[i]).style.display = 'block';
else
document.getElementById(arrDivs[i]).style.display = 'none';
}
window.onload = function() {
var current, eSelect = document.getElementById('group');
eSelect.onchange = function() {
var id = eSelect.value;
if (current && current != id) {
document.getElementById(current).style.display = 'none'
}
document.getElementById(id).style.display = 'block';
current = id;
}
}
I'm a javascript novice myself, but what about using a current variable to show which div is shown (and therefore knowing which div to hide)? 我本人是一名Javascript新手,但是如何使用当前变量显示显示的div(并因此知道要隐藏的div)呢?
window.onload = function() {
var current, eSelect = document.getElementById('group');
eSelect.onchange = function() {
var id = eSelect.value;
if (current && current != id) {
document.getElementById(current).style.display = 'none'
}
document.getElementById(id).style.display = 'block';
current = id;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.