简体   繁体   中英

Change first option of select

I have two date inputs & one drop down with first option "All"..

My problem is when I select dates, if both dates are same, then the first option of select should become blank.

<script type='text/javascript'>
jQuery(document).ready(function () {
    var first = jQuery('#category').find('option').first();
    jQuery('#date_to').on('change', function (e) {
    if($('#date_to').val == $('#date_from').val) {
        first.remove();
    })
});
</script>

在此处输入图片说明

Change val to val() because it is a method. You also need to move the ) of the if statement to the correct place;

if ($('#date_to').val() == $('#date_from').val()) {
    first.remove();
}

As you want to remove first option you are required to put on change on both date pickers because user can pick any date and every time a user picks a date condition is executed. Well that is up to your need also.

Following is the working code.

<script type='text/javascript'>
jQuery(document).ready(function () {
    var first = jQuery('#category').find('option').first();

    $('#date_to').on('change', function (e) {
        remove();
    });

    $('#date_from').on('change', function (e) {
        remove();
    });

    function remove () {
        if($('#date_to').val() == $('#date_from').val()) {
            first.remove();
        };
    }
});
</script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM