Hide only those Select Box which has null value selected

后端 未结 3 1699
孤街浪徒
孤街浪徒 2021-01-26 14:11

I want to hide only those select box which has a null value selected. I have following HTML and jQuery

提交评论

  • 2021-01-26 14:38

    You can use the atrribute selector to find elements that have a selected value :

    $('.date-month option[selected="selected"]').parent().hide();
    

    Show/hide selects :

     $('.date-month').each(function() {
        if ($(this).find('option[selected="selected"]').length>0) {
           $(this).hide();
        } else {
           $(this).show();
        }
      });
    
    0 讨论(0)
  • 2021-01-26 14:48

    $(document).ready(function () {
                $('.date-month').each(function() {
                    if ($(this).val()=='')
                      $(this).hide();
                     
                });
            });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <div class="form-item">
    <select name="select1" class="date-month">
    <option value="">select</option>
    <option value="1">Jan</option>
    <option value="2">Feb</option>
    <option value="3">Mar</option>
    <option value="4">Apr</option>
    <option value="5" selected="selected">May</option>
    <option value="6">Jun</option>
    <option value="7">Jul</option>
    <option value="8">Aug</option>
    </select>
    </div>
    <div class="form-item">
    <select name="select2" class="date-month">
    <option value="">select</option>
    <option value="1">Jan</option>
    <option value="2">Feb</option>
    <option value="3">Mar</option>
    <option value="4">Apr</option>
    <option value="5">May</option>
    <option value="6">Jun</option>
    <option value="7">Jul</option>
    <option value="8">Aug</option>
    </select>
    </div>

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