简体   繁体   中英

How to Fire an Event with Jquery when a select value is unchanged for a specific option or value is changed to a specific option

So I Want my page to redirect to different page when a specific value of select is chosen. Whether it changes the value or not. If the value is already some other value the on Change works fine but if the value is already selected as the value which redirects the page it doesn't works for example

<script>
$("select").on('change', function() {
        if ($(this).val() == 'option2') {
            var data = $(this).attr("data_val");
            var sid = $(this).parent().parent().children()[3];
            var sid_value = $(sid).children()[0].value;
            window.location = 'new.php?data=' + data + '&sid=' +   sid_value;
        }});
</script>

if the select is set to option1 and i select option2 from the dropdown then the page redirects but when the option2 is already selected in the select and i open the dropdown again & click on option2 it doesn't redirect, please help me to achieve this.

Well i found a Workaround for this problem i'll post it here if anyone faces the same problem. Here's the code

    <Script>
    var click1Done = false;
        $("select").on('click', function() {
            customSelect($(this));
            click1Done = true;
        });

        function customSelect(obj) {
            el = obj[0];
            if (click1Done && el.value === 'option2') {
                click1Done = false;
                //your code here to perform any extra task    
                window.location = 'new.php';
            }
        }</script>

Here i am seeing the no. of click on select if it is the second click on the desired option ('here option2') i redirect to the 2nd page else i don't.

consider this

$("select option").on('click', function () {
 // your code here
});

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