Can I apply the required attribute to <select> fields in HTML5?

前端 未结 13 2280
不知归路
不知归路 2020-11-22 16:20

How can I check if a user has selected something from a doesn\'t support the new

相关标签:
13条回答
  • 2020-11-22 16:50

    Make the value of first item of selection box to blank.

    So when every you post the FORM you get blank value and using this way you would know that user hasn't selected anything from dropdown.

    <select name="user_role" required>
        <option value="">-Select-</option>
        <option value="User">User</option>
        <option value="Admin">Admin</option>
    </select>
    
    0 讨论(0)
  • 2020-11-22 16:52

    In html5 you can do using the full expression:

    <select required="required">
    

    I don't know why the short expression doesn't work, but try this one. It will solve.

    0 讨论(0)
  • 2020-11-22 16:52

    You can do it also dynamically with JQuery

    Set required

    $("#select1").attr('required', 'required');
    

    Remove required

    $("#select1").removeAttr('required');
    
    0 讨论(0)
  • 2020-11-22 16:54
    <form action="">
    
    <select required>
    
      <option selected disabled value="">choose</option>
      <option value="red">red</option>
      <option value="yellow">yellow</option>
      <option value="green">green</option>
      <option value="grey">grey</option>
    
    </select>
    <input type="submit">
    </form>
    
    0 讨论(0)
  • 2020-11-22 16:54

    first you have to assign blank value in first option. i.e. Select here.than only required will work.

    0 讨论(0)
  • 2020-11-22 16:54

    try this, this gonna work, I have tried this and this works.

    <!DOCTYPE html>
    <html>
    <body>
    
    <form action="#">
    <select required>
      <option value="">None</option>
      <option value="volvo">Volvo</option>
      <option value="saab">Saab</option>
      <option value="mercedes">Mercedes</option>
      <option value="audi">Audi</option>
    </select>
    <input type="submit">
    </form>
    
    </body>
    </html>
    
    0 讨论(0)
提交回复
热议问题