【问题标题】:jQuery traversing form elementsjQuery遍历表单元素
【发布时间】:2009-10-22 05:24:06
【问题描述】:

默认情况下,我试图隐藏表单标签和文本框,并在选择选项时显示它们。我不明白为什么我的代码总是失败。

<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 不选择我的元素? >.

【问题讨论】:

    标签: jquery forms select traversal


    【解决方案1】:

    值得注意的是HTML 4.01 specification

    IDNAME 标记必须以字母 ([A-Za-z]) 开头,并且可以是 后跟任意数量的字母, 数字([0-9]),连字符(“-”), 下划线 ("_")、冒号 (":") 和 句点(“.”)。

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

    【讨论】:

    • id 实际上不是数字。我只是用它们来防止人们关注不相关的信息。事后看来,坏主意 xD 我似乎也粘贴了旧代码 >_> 最后两个 TD 应该是:
    • 你想要 class="otherHide" 而不是 class=".otherHide"。句点仅在 CSS 选择器中使用,表示您正在寻找一个类。
    • ...盯着这段代码看了半个小时,试图弄清楚为什么 jQuery 不能很好地运行,结果却是 HTML 中的一个简单错误>_> 感谢您的帮助; )
    猜你喜欢
    • 2011-10-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多