javascript - 隐藏/显示 Div onChange

标签 javascript html

我有各种带有表单字段的 div。基于下拉列表,我需要它显示与该值对应的 div,并隐藏所有其他 div。 (也许有一种更有效的方法可以做到这一点,而不是加载所有 div 并隐藏它们,我不确定)。

我现在拥有的是:

<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>

以及当前半工作的 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>

我得到的是“id”为空,所以我还无法完成该部分。

第 2 部分将隐藏旧的 div 并显示新的 div(如果他们要更改所选的选项)。有什么建议吗?

解决方案:

<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>

为了制作选择和 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>';
}

最佳答案

尝试使用

var id = eSelect.value;

对于第二部分,如果您使用纯 javascript(我的意思是,您没有使用像 jQuery 这样的 javascript 框架),也许一个好的解决方案是加载包含 div 的 ids 的数组并迭代它,隐藏 div ! = eSelect.value

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';
}

关于javascript - 隐藏/显示 Div onChange,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7919806/

相关文章:

javascript - Jquery 将下拉列表中的值添加到文本输入

javascript - 如何通过 appcelerator 中的 JSON 解析从对象数组中检索值

javascript - 如何将保存 Html 的 Javascript 变量转换为字符串?

javascript - 模型personalDeatail.add 值中的数据不会自动更新。我究竟做错了什么?

python - Flask 和 AngularJs

javascript - 如何启用超链接以打开模态对话框,同时还允许在同一链接上的新选项卡中打开

javascript - 选中复选框以移至下一页

javascript - 如何计算欧氏距离? (华硕)

javascript - AngularJS - 没有获得 Controller 或 View 的返回值

javascript - 我想先显示图片然后我想显示网站内容