如何在INPUT标记上没有ID属性的情况下使用LABEL标记的FOR属性

Sha*_*awn 65 html label for-loop input

是否有解决下面代码中说明的问题的方法?首先在浏览器中打开代码直接到达关键点,而不必在知道您要查找的内容之前查看所有代码.

<html>
 <head>
  <title>Input ID creates problems</title>
  <style type="text/css">
   #prologue, #summary { margin: 5em; }
  </style>
 </head>
 <body>
  <h1>Input ID creates a bug</h1>
  <p id="prologue">
   In this example, I make a list of checkboxes representing things which could appear in a book. If you want some in your book, you check them:
  </p>
  <form>
   <ul>
    <li>
     <input type="checkbox" id="prologue" />
     <label for="prologue">prologue</label>
    </li>
    <li>
     <input type="checkbox" id="chapter" />
     <label for="chapter">chapter</label>
    </li>
    <li>
     <input type="checkbox" id="summary" />
     <label for="summary">summary</label>
    </li>
    <li>
     <input type="checkbox" id="etc" />
     <label for="etc">etc</label>
     <label>
    </li>
   </ul>
  </form>
  <p id="summary">
   For each checkbox, I want to assign an ID so that clicking a label checks the corresponding checkbox. The problems occur when other elements in the page already use those IDs. In this case, a CSS declaration was made to add margins to the two paragraphs which IDs are "prologue" and "summary", but because of the IDs given to the checkboxes, the checkboxes named "prologue" and "summary" are also affected by this declaration. The following links simply call a javascript function which writes out the element whose id is <a href="javascript:alert(document.getElementById('prologue'));">prologue</a> and <a href="javascript:alert(document.getElementById('summary'));">summary</a>, respectively. In the first case (prologue), the script writes out [object HTMLParagraphElement], because the first element found with id "prologue" is a paragraph. But in the second case (summary), the script writes out [object HTMLInputElement] because the first element found with id "summary" is an input. In the case of another script, the consequences of this mix up could have been much more dramatic. Now try clicking on the label prologue in the list above. It does not check the checkbox as clicking on any other label. This is because it finds the paragraph whose ID is also "prologue" and tries to check that instead. By the way, if there were another checkbox whose id was "prologue", then clicking on the label would check the one which appears first in the code.
  </p>
  <p>
   An easy fix for this would be to chose other IDs for the checkboxes, but this doesn't apply if these IDs are given dynamically, by a php script for example.
   Another easy fix for this would be to write labels like this:
   <pre>
    &lt;label&gt;&lt;input type="checkbox" /&gt;prologue&lt;/label&gt;
   </pre>
   and not need to give an ID to the checkboxes. But this only works if the label and checkbox are next to each other.
  </p>
  <p>
   Well, that's the problem. I guess the ideal solution would be to link a label to a checkboxe using another mechanism (not using ID). I think the perfect way to do this would be to match a label to the input element whose NAME (not ID) is the same as the label's FOR attribute. What do you think?
  </p>
 </body>
</html>
Run Code Online (Sandbox Code Playgroud)

小智 126

它在这里得到了解决:https: //stackoverflow.com/a/8537641 就这样做了

<label><input type="checkbox">Some text</label>
Run Code Online (Sandbox Code Playgroud)

  • 这仅在标签和复选框彼此相邻时才有效. (9认同)
  • @Shawn原帖是什么情况. (8认同)
  • 问题是你需要设计它的样式.CSS中没有父选择器. (5认同)

Dra*_*ter 14

在我看来,你能做的最好的,就是重命名所有的复选框,通过在它们的id中添加一些前缀,例如 input

   <ul>
    <li>
     <input type="checkbox" id="input_prologue" />
     <label for="input_prologue">prologue</label>
    </li>
    <li>
     <input type="checkbox" id="input_chapter" />
     <label for="input_chapter">chapter</label>
    </li>
    <li>
     <input type="checkbox" id="input_summary" />
     <label for="input_summary">summary</label>
    </li>
    <li>
     <input type="checkbox" id="input_etc" />
     <label for="input_etc">etc</label>
    </li>
   </ul>
Run Code Online (Sandbox Code Playgroud)

这样,您就不会与页面上的其他ID发生任何冲突,单击标签将切换复选框,而不需要任何特殊的javascript函数.


Eri*_*and 7

编辑:回想起来,我的解决方案远非理想.我建议您改为利用"隐式标签关联",如下所示:stackoverflow.com/a/8537641/884734

我提出的不太理想的解决方案如下:

用一点点javascript就可以轻松解决这个问题.只需在您的一个页面的js文件中抛出以下代码,即可为<label>标记提供以下行为:

单击标签时:

如果页面上有一个id与标签for属性匹配的元素,请恢复默认功能并关注该输入.

如果使用未找到匹配id,寻找的兄弟姐妹label有一个class匹配labelfor属性,并将重点.

这意味着您可以像这样布置表单:

<form>
    <label for="login-validation-form-email">Email Address:</label>
    <input type="text" class="login-validation-form-email" />
</form>
Run Code Online (Sandbox Code Playgroud)

唉,实际代码:

$(function(){
    $('body').on('click', 'label', function(e){
        var labelFor = $( this ).attr('for');
        if( !document.getElementById(labelFor) ){
            e.preventDefault(); e.stopPropagation();
            var input = $( this ).siblings('.'+labelFor);
            if( input )
                input[0].focus();
        }
    })
});
Run Code Online (Sandbox Code Playgroud)

注意:根据W3C规范验证站点时,这可能会导致问题,因为该<label> for属性应始终在页面上具有匹配ID的相应元素.

希望这可以帮助!

  • 这不仅会导致 W3C 符合验证器出现问题,而且也不适用于辅助技术,例如屏幕阅读器。正如您在评论中所写,他们需要隐式标签关联。 (2认同)