当你重新加载页面时...所有脚本的运行方式与第一次加载时相同。以前的加载没有保存或访问。
然而,您可以将数据存储在Cookie或localStorage中,并在页面加载时访问该数据。
试着这么做:
$(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)
});
更好的解决办法可能是使用AJAX来更新您的购物车存放在服务器上,并设置所有存储的值存在时,页面加载
这工作完全谢谢你!有没有办法在JS运行时隐藏它,然后在完成时显示它? – xohbrittx
隐藏下拉菜单中的完整下拉菜单或显示值为空? –
我调整了代码,我用这个替换了代码: 隐藏下拉列表中的完整下拉列表或显示值为空? 我还想让你做POC,我通过删除代码来调整代码: $(“。ProductDetails select”)。find(“option”)。each(function(){ if($ (this).val()== selectedProduct){ $(this).attr(“selected”,true); } }); 并放置 $(“。ProductDetails select”)。val(selectedProduct) 如果您能够提交表单,请尝试。 –