【问题标题】: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当特定选项的选择值未更改或值更改为特定选项时,如何使用 Jquery 触发事件
【发布时间】:2017-01-10 10:29:55
【问题描述】:

所以我希望我的页面在选择特定值时重定向到不同的页面。是否改变值。如果该值已经是其他值,则 on Change 可以正常工作,但如果该值已被选为重定向页面的值,则它不起作用

<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实现这一目标。

【问题讨论】:

标签: javascript jquery select onchange url-redirection


【解决方案1】:

好吧,我找到了解决此问题的方法,如果有人遇到同样的问题,我会在此处发布。这是代码

    <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 option2')我重定向到第二页,否则我没有。

【讨论】:

    【解决方案2】:

    考虑一下

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-02-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多