Hide only those Select Box which has null value selected

后端 未结 3 1055
梦谈多话
梦谈多话 2021-01-26 14:05

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

提交评论

  • 2021-01-26 14:44

    $(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)
  • 2021-01-26 14:45

    One line should be enough:

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

    https://jsfiddle.net/wh27868m/1/

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