选择onchange重新加载页面并保留选择的选项 [英] Select onchange reload the page and keep the selected option
问题描述
我有这个 https://jsfiddle.net/1zqgeq79/2/
这是我用来更改选择后刷新页面的jQuery,现在我只需要它即可在页面加载后保留该选择的选项.
$('.ProductDetails select').change(function () {
location.reload();
});
由于我有多个项目,因此我知道必须使用(this),但我仍在学习jquery.感谢您的帮助!
如果您不想在关闭浏览器后永久保留数据,请使用sessionStorage而不是localStorage.
我将代码替换为:
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();
});
只需转到: https://jsfiddle.net/1zqgeq79/3/ >
我只是为第一个下拉菜单所做的.
I have this 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.
$('.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. Thanks for the help!
Use sessionStorage instead of localStorage if you don't want to keep the data parmanently after closing the browser.
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/
I did for first dropdown only.
这篇关于选择onchange重新加载页面并保留选择的选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!