2

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

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

4

2 回答 2

2

隐藏:

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

以显示:

document.getElementById('categories').style.display=""
于 2012-11-21T17:17:42.450 回答
-1

最好使用 jquery 的 .toggle() 方法。您可以节省时间并且更好,因为您可以制作效果

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

你可以做

$(#categories).toggle(); //to show or hide the id = "categories"
于 2012-11-21T17:23:30.927 回答