问题
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