2012-11-21 158 views
3

我需要一些帮助。我试图在选择跳转菜单时显示/隐藏div内容。我似乎无法让代码工作。这里是我的代码:使用JavaScript显示/隐藏Div内容

JS: 

function toggleOther(chosen){ 
if (chosen == 'cat') { 
    document.getElementById('categories').style.visibility = 'visible'; 
} else { 
    document.getElementById('categories').style.visibility = 'hidden'; 
    document.myform.other.value = ''; 
} 

And my page: 

<td class="formlabel" nowrap="nowrap"><form name="Users">Users:</td> 
<td nowrap="nowrap" class="formlabel"><select name="fieldname" size="1" onchange="toggleOther(document.myform.values.options[document.myform.values.selectedIndex ].value);" class="select" > 
    <option selected="selected">Choose</option> 
    <option>All Users</option> 
    <option value="cat">User 1</option> 
    <option value="cat">User 2</option> 
    <option value="cat">User 3</option> 
    <option value="cat">User 4</option> 
    <option value="cat">User 5</option> 
    </select></td> 
<div style="opacity: 0.3; background: #fff; display:none"> 
<td width="380" valign="top" class="center"> 
<table width="360" class="imagetable" cellpadding="0" cellspacing="0" id="categories"> 
    <tr> 
    <th colspan="2" nowrap="nowrap" class="reportname">Categories</th> 
    </tr> 
    <tr> 
    <td class="formlabel" nowrap="nowrap"><form name="Exams">Exams</td> 
    <td nowrap="nowrap" class="formlabel"><select name="fieldname" size="1" class="select"> 
     <option value="#" selected="selected" target="_blank">Choose</option> 
     <option value="">All Exams</option> 
     <option value="">Exam 1</option> 
     <option value="">Exam 2</option> 
     <option value="">Exam 3</option> 
     <option value="">Exam 4</option> 
     <option value="">Exam 5</option> 
     </select></td> 
     </tr> 
    </form> 
     <tr> 
     <td nowrap="nowrap" class="formlabel">Include Categories</td> 
     <td nowrap="nowrap" class="formlabel"><input type="text" name="textfield2" id="textfield2" class="fields" size="4" />or more items assigned</td> 
     </tr> 
     <tr> 
     <td class="formlabel" nowrap="nowrap">Display Categories</td> 
     <td nowrap="nowrap" class="formlabel">that appear 
      <input type="text" name="textfield3" id="textfield3" class="fields" size="4" />or more exams</td> 
     </tr> 
     </table> 

这里有什么帮助吗?我似乎无法得到它的工作...

+2

尝试使用设置'display' CSS属性'none'隐藏它和'” “'使其可见。 – Maz

+0

什么元素有id **类别**? –

回答

3

要隐藏:

document.getElementById('categories').style.display="none" 

要显示:

document.getElementById('categories').style.display="" 
+0

这很好。谢谢! – boy

+1

很高兴帮助:) –

-1

这是更好地使用jQuery的.toggle()方法。节省您的时间,这是因为漂亮,你可以做的效果

http://api.jquery.com/toggle/

你可以只是做

$(#categories).toggle(); //to show or hide the id = "categories" 
+1

谢谢!我会看看! – boy

+2

-1用于暗示使用一个框架来做这样一个简单的事情,就像隐藏div一样。 – Sebas