简体   繁体   English

Javascript onchange function 在移动设备上的 select 菜单中不起作用

[英]Javascript onchange function doesn't work in select menu on mobile

            <script type="text/javascript"> 
            var urlmenu = document.getElementById( "rpdropdown3" );
            urlmenu.remove(1);
            urlmenu.onchange = function() {
            window.open( this.options[ this.selectedIndex ].value, "_blank" );
            var options = rpdropdown3.options;
                // Look for a default selected option
                for (var i=0, iLen=options.length; i<iLen; i++) {

                    if (options[i].defaultSelected) {
                        rpdropdown3.selectedIndex = i;
                        return;
                    }                                                 
                }

                // If no option is the default, select first or none as appropriate
                selectElement.selectedIndex = 0; // or -1 for no option selected
            };

            </script>';

$postslist = get_posts( $args );
    foreach( $postslist as $posts ){
        $string .= '<option value="' . get_the_permalink($posts) . '">-- '  .get_the_title($posts).'</option> ';
    }

Doesn't work on any mobile browser (works on computers browsers), the select dropdown doesn't open the link as expected不适用于任何移动浏览器(适用于计算机浏览器),select 下拉菜单无法按预期打开链接

It seems that It's actually working but it's blocked as a popup.似乎它实际上正在工作,但它被阻止为弹出窗口。 You can't use window.open if it's not fired by a user action, like a button or a link.如果 window.open 不是由用户操作(如按钮或链接)触发,则不能使用它。

foreach ($postslist as $posts) { ?>
    <select id="rpdropdown" onchange="if (this.value) window.location.href=this.value">
        <option value="" selected disabled>OPTION 1</option>
        <option value="<?php echo get_the_permalink($posts) ?>" onchange="window.open (this.value)"> -- <?php echo get_the_title($posts) ?></option>
    </select>

<?php }

In my case I used onchange="if (this.value) window.location.href=this.value" and I'm trying to make it open in a new tab.在我的情况下,我使用了onchange="if (this.value) window.location.href=this.value"并且我试图让它在新标签中打开。

They talk about that issue here and on others many posts: javascript window.open in safari他们在这里和其他许多帖子上谈论这个问题: javascript window.open in safari

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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