How to check whether a select box is empty using JQuery/Javascript

前端 未结 3 1509
感动是毒
感动是毒 2020-12-24 11:59

I have a select box that is being populated dynamically from a database. As of right now the population of this check box is working flawlessly.

I have added functio

相关标签:
3条回答
  • 2020-12-24 12:07

    Another correct way to get selected value would be using this selector:

    $("option[value="0"]:selected")
    

    Best for you!

    0 讨论(0)
  • 2020-12-24 12:10

    To check whether select box has any values:

    if( $('#fruit_name').has('option').length > 0 ) {
    

    To check whether selected value is empty:

    if( !$('#fruit_name').val() ) { 
    
    0 讨论(0)
  • 2020-12-24 12:30

    One correct way to get selected value would be

    var selected_value = $('#fruit_name').val()
    

    And then you should do

    if(selected_value) { ... }
    
    0 讨论(0)
提交回复
热议问题