用JavaScript显示/隐藏Div内容

时间:2022-08-24 16:19:28

I need a little help here. I am trying to show / hide div content on select of a jump menu. I can't seem to get the code to work. Here is my code:

我需要一点帮助。我试图在“选择跳转”菜单上显示/隐藏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>

Any help here? I cannot seem to get it to work...

任何帮助吗?我似乎无法让它发挥作用……

2 个解决方案

#1


3  

To hide:

隐藏:

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

To show:

显示:

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

#2


-1  

It is better to use the jquery's .toggle() method. You save time and it is nicer since you can do effects

最好使用jquery的.toggle()方法。你可以节省时间,而且效果更好

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

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

you could just do

你可以做

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

#1


3  

To hide:

隐藏:

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

To show:

显示:

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

#2


-1  

It is better to use the jquery's .toggle() method. You save time and it is nicer since you can do effects

最好使用jquery的.toggle()方法。你可以节省时间,而且效果更好

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

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

you could just do

你可以做

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