简体   繁体   English

重置选择框不适用于iPad,但适用于Windows上的Chrome

[英]Resetting a select box not working on iPad, but works on Chrome on Windows

Here's the Javascript code I'm using right now. 这是我现在正在使用的Javascript代码。

$(document).ready(function(){
    $('#altNav').change(function(){
        var altNavSelectedVal = $('#altNav').val();
        if (altNavSelectedVal != 'currentPage')
        {
            $('#altNav option[value="currentPage"]').attr('selected', 'selected');
            location.href = altNavSelectedVal;
        }
    });
});

It's used to reset a select box with the ID altNav to the option with the value currentPage before navigating to the next page. 它用于在导航到下一页之前将ID为altNav的选择框重置为值为currentPage的选项。

This is to prevent the browser from caching its previous value. 这是为了防止浏览器缓存其先前的值。 For example, if I'm on the About page and I change it to Services, it will take me to the Services page. 例如,如果我在“关于”页面上并将其更改为“服务”,它将带我进入“服务”页面。 If I hit the browser back button it will go back to the About page but will have its active selection be Services, since it was changed to services just before it changed the page. 如果单击浏览器后退按钮,它将返回到“关于”页面,但其活动选择是“服务”,因为它在更改页面之前已更改为“服务”。

The HTML code is below. HTML代码如下。

<select id="altNav">
    <option value="/">Home</option>
    <option value="/services">Services</option>
    <option value="currentPage" selected="selected">Contact</option>
    <option value="/about">About</option>
    <option value="/news">News</option>
</select>

Normally the Contact option would have a value of /contact if it was on another page, but it's different for each page. 通常,如果在另一页上,“联系方式”选项的值为/contact ,但每个页面都不同。 The normal values are / for Home, /services , /contact , /about , and /news , but when it's on one of those page, that option's value is set to currentPage . 对于Home, /services/contact/about/news ,正常值是/ ,但是当在这些页面之一上时,该选项的值设置为currentPage

Anyway, the code works as it's supposed to on Chrome for Windows, but when I test on iPad (and probably iPhone), when hitting the back button it always returns to Home. 无论如何,该代码可以像在Windows版Chrome浏览器上那样正常工作,但是当我在iPad(可能还有iPhone)上进行测试时,按下“后退”按钮时,它始终会返回“首页”。

When I comment out the line location.href = altNavSelectedVal; 当我注释掉该行的location.href = altNavSelectedVal; it should, if I for example am on the News page and select Services, switch back to News after I select Services, but it actually selects Home. 例如,如果我在“新闻”页面上并选择“服务”,则应该在选择“服务”后切换回“新闻”,但实际上是选择“主页”。 I don't understand why it would only do this on iOS. 我不明白为什么只能在iOS上执行此操作。

I don't have an ipad/iphone but you may need to remove the old selected with 我没有ipad / iphone,但您可能需要使用

$('#altNav').removeAttr('selected');

http://jsfiddle.net/un45t/ http://jsfiddle.net/un45t/

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

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