2009-10-22 207 views
0

我试图隐藏表单标签和文本框,默认情况下,并使它们出现时,选择一个选项。我看不出为什么我的代码保持失败。jQuery遍历表单元素

<tr> 
    <td><label>My Label</label></td> 
    <td> 
     <select name="1" id="1"> 
     <option value="2" selected="selected">2</option> 
     <option value="3">3</option> 
     <option value="4">4</option> 

     <option value="Other">Other</option> 
     </select> 
    </td> 
    <td><label class=".otherHide">Other</label></td> 
    <td><input class=".otherHide" type="text" name="otherSpec" id="otherSpec" /></td> 
</tr> 

jQuery(document).ready(function() 
{ 
    jQuery('#1').change(function() 
    { 
     jQuery('#1 option:selected').each(function() 
     { 
     if (jQuery(this).text() == 'Other') 
     { 
      jQuery('.otherHide').each(function() 
      { 
       jQuery(this).animate({opacity: 0.0}, 2000); 
      }); 
     } 
     }); 
    }).change(); 
}); 

它目前设置为隐藏表单元素,为什么我测试。

我原本没有使用类,并试图通过它遍历我的手。如果不这样做,我就会上课,最终失败。

为什么jQuery不选择我的元素? >。 <

回答

1

值得从HTML 4.01 specification注意到:

IDNAME令牌必须以字母开头([A-ZA-Z]),并且可以是 ,接着任意数量的字母, 数字([0-9]),连字符(“ - ”), 下划线(“_”),冒号(“:”)和 句点(“。”)。

除此之外,HTML中没有类otherHide的元素。

+0

这个ID实际上并不是数字。我只是用它们来阻止人们关注不相关的信息。现在回想起来,坏主意的xD 我似乎也粘贴旧代码> _> 最后两个TD的应为: ​​<标签类=”。otherHide “>其他 ​​<输入类=” otherHide。 “type =”text“name =”otherSpec“id =”otherSpec“/> – Kane 2009-10-22 05:38:23

+2

你想要class =”otherHide“而不是class =”。otherHide“。该时间段仅用于CSS选择器以表明您正在寻找一个班级。 – Gareth 2009-10-22 05:50:02

+0

...半小时盯着这段代码试图弄清楚为什么jQuery不能很好地播放,并且它变成了HTML> _> 感谢您的帮助;) – Kane 2009-10-22 05:54:58