Validate select box

后端 未结 8 632
广开言路
广开言路 2020-12-08 01:44

I\'m using the jQuery plugin Validation to validate a form. I have a select list looking like this:


                        
    
提交评论

  • 2020-12-08 02:40

    For starters, you can "disable" the option from being selected accidentally by users:

    <option value="" disabled="disabled">Choose an option</option>
    

    Then, inside your JavaScript event (doesn't matter whether it is jQuery or JavaScript), for your form to validate whether it is set, do:

    select = document.getElementById('select'); // or in jQuery use: select = this;
    if (select.value) {
      // value is set to a valid option, so submit form
      return true;
    }
    return false;
    

    Or something to that effect.

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