简体   繁体   English

选择onchange重新加载页面并保留选定的选项

[英]Select onchange reload the page and keep the selected option

I have this https://jsfiddle.net/1zqgeq79/2/ 我有这个https://jsfiddle.net/1zqgeq79/2/

This is the jquery I'm using to make the page refresh when a select has been changed and now I just need it to keep that selected option after the page loads. 这是我用来在更改选择时刷新页面的jquery,现在我只需要在页面加载后保留选定的选项。

       $('.ProductDetails select').change(function () {
         location.reload();
       });

Since I have multiple items I know a (this) will have to be used, but I'm still learning jquery. 由于我有多个项目,我知道必须使用(这个),但我还在学习jquery。 Thanks for the help! 谢谢您的帮助!

Use sessionStorage instead of localStorage if you don't want to keep the data parmanently after closing the browser. 如果您不想在关闭浏览器后保持数据的持久性,请使用sessionStorage而不是localStorage。

I replaced the code with: 我用以下代码替换了代码:

var selectedProduct = sessionStorage.getItem("product");
if(selectedProduct != undefined || selectedProduct != null){
    $(".ProductDetails select").first().find(":selected").removeAttr("selected");
  $(".ProductDetails select").find("option").each(function () {
            if ($(this).val() == selectedProduct) {
                $(this).attr("selected", true);
            }
        });
}

$('.ProductDetails select').change(function () {
    sessionStorage.setItem("product", $(".ProductDetails select").first().val());

  location.reload();
});

Just go to: https://jsfiddle.net/1zqgeq79/3/ 请访问: https//jsfiddle.net/1zqgeq79/3/

I did for first dropdown only. 我只做了第一次下拉。

When you reload the page... all your script runs the same way as when you loaded it the first time. 当您重新加载页面时...所有脚本的运行方式与第一次加载时的运行方式相同。 Nothing is saved or accessible from previous load. 以前的加载中没有保存或访问任何内容。

You can however store data in cookies or localStorage and access that when page loads. 但是,您可以将数据存储在cookie或localStorage中,并在页面加载时访问该数据。

Try something like: 尝试类似的东西:

$(function(){
    // get stored value or make it empty string if not available
    var storedValue = localStorage.getItem('prod-detail') || '';

    $('.ProductDetails select').change(function () {
         // store current value
         var currValue = $(this).val();
         localStorage.setItem('prod-detail', currValue );
         // now reload and all this code runs again
         location.reload();
    })
    // set stored value when page loads
    .val(storedValue)

});

A better solution might be to use ajax to update your cart stored on server and set all stored values there when page loads 更好的解决方案可能是使用ajax更新存储在服务器上的购物车,并在页面加载时设置所有存储的值

You can either add a query string parameter before reloading. 您可以在重新加载之前添加查询字符串参数。

var url = document.location;
document.location = url + "?select=" + $(this).val();

So when the page is loading you can read this parameter and act as you want. 因此,当页面加载时,您可以读取此参数并按您的意愿操作。

You can also do this using a cookie. 您也可以使用cookie执行此操作。 jQuery allows you do do that. jQuery允许你这样做。 Check out this link: http://plugins.jquery.com/cookie/ 看看这个链接: http//plugins.jquery.com/cookie/

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

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