jquery selectedIndex doesn't work

前端 未结 3 1095
耶瑟儿~
耶瑟儿~ 2021-01-28 18:20

i have a from with many select tags , when the user submit the form i want to check if the user choose one option for all the select tags and this is my jquery code



        
相关标签:
3条回答
  • 2021-01-28 18:53

    It's this.selectedIndex, not $this.selectedIndex :)

    • this is the HTMLDomElement
    • $this is jQuery object
    0 讨论(0)
  • 2021-01-28 18:57
    var $this = $(this);
    if($this.get(0).selectedIndex == 0) {
    
    }
    

    or just simple

    this.selectedIndex; // not $this / $(this)
    

    If no option it will return -1

    here I showed all above cases

    0 讨论(0)
  • 2021-01-28 19:09

    If you are using a version of jQuery above 1.6? then you can use

    if ( $this.prop('selectedindex') == 0 ){ /* handle */ }
    
    0 讨论(0)
提交回复
热议问题