How to have one label for multiple select boxes?

前端 未结 6 1921
广开言路
广开言路 2020-12-20 16:37

I have on this check in form:



                        
    
提交评论

  • 2020-12-20 16:51

    There is no proper way; a label refers to one element. Just point it to the first one.

    <label for="day">Check in date </label>
    

    You could also use a specifically-styled <fieldset> if you like semantics, but I think that's a bit overkill. An <input type="date"> is probably the best option here, as it is one element that can be pointed to by your <label>, is more semantic, and can be somewhat friendlier if you implement a good date picker to go along with it.

    If you want to stick with the <select>s, try giving each one a title attribute for accessibility.

    0 讨论(0)
  • 2020-12-20 16:59

    Trying to improve @Bracketworks answer:

    <label id="date">Check in date</label>
    <label for="day" id="label_day">Day</label>
    <select id="day" aria-labelledby="date label_day">
        <!-- ... -->
    </select> 
    <label for="month" id="label_month">Month</label>
    <select id="month" aria-labelledby="date label_month">
        <!-- ... -->
    </select> 
    <label for="year" id="label_year">Year</label>
    <select id="year" aria-labelledby="date label_year">
        <!-- ... -->
    </select>
    

    See example 1 of MDN's "Using the aria-labelledby attribute".

    0 讨论(0)
  • 2020-12-20 17:03

    Following with the answer from @Alohci, you can also use aria-labelledby and reverse the naming reference (which I think is a bit closer to the convention you were looking for):

    <label id="date">Check in date</label>
    <select aria-labelledby="date">
        <!-- ... -->
    </select> 
    <select aria-labelledby="date">
        <!-- ... -->
    </select> 
    <select aria-labelledby="date">
        <!-- ... -->
    </select>
    

    Also note, as per the W3C on labelled-by:

    If the label text is visible on screen, authors SHOULD use aria-labelledby and SHOULD NOT use aria-label. Use aria-label only if the interface is such that it is not possible to have a visible label on the screen. User agents give precedence to aria-labelledby over aria-label when computing the accessible name property.

    0 讨论(0)
  • 2020-12-20 17:04

    You cannot associate a label element with more than one control. This is described in the definition of label.

    You could give each select element its own label.

    A better approach is to have a single text input field for a date. Then there is no problem with label. It means more work, since you have to parse the data server-side, and you should also parse it client-side (for checks, so that the user can immediately be informed of problems). But it is better usability (surely it is faster to type in a date than to use three clumsy dropdowns) and better accessibility. You need to decide on a date format and clearly tell the user what the expected format is.

    0 讨论(0)
  • 2020-12-20 17:04

    HTML5's input type="date" might be useful too, particularly if you're using month/day/year select boxes as a way to limit date selection possibilities. This input element supports min and max date attributes, so you can apply your limitations. It's not supported by older browsers, but I've seen smart cookies use jQueryUI's datepicker as a shim (by using capabilities detection to determine type="date" support, then loading in and invoking the datepicker only if it isn't supported natively).

    0 讨论(0)
  • 提交回复
    热议问题