jQuery bind method does not work as expected

淺唱寂寞╮ 提交于 2021-01-29 15:11:43

问题


I need to perform different actions for a select dropdown if it is selected or not! I tried this code which is working correctly in a JSFiddle, but it doesn't work within my project which is a checkout form of WooCommerce:

jQuery(document).ready(function() {
  jQuery("#my_datepicker").bind('change', function() {
    if (jQuery(this).val() != "select") {
      alert('Something is selected!');
    } else {
      alert('Nothing is selected!');
    }
  });

  jQuery("#my_datepicker").trigger('change');
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<select name="my_datepicker" id="my_datepicker" class="select valid" data-placeholder="Please select" aria-invalid="false">
  <option value="select">Please select</option>
  <option value="11th December">11th December</option>
  <option value="12th December">12th December</option>
</select>

In my project alert('Nothing is selected!'); works on page load as expected but after changing select options nothing happens! Even when I choose <option value="select">Please select</option> again, it's related alert does not appear. Can you please guide me about this issue?

来源:https://stackoverflow.com/questions/65789631/jquery-bind-method-does-not-work-as-expected

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!