Javascript 未设置本地存储默认选项的页面加载Jquery
我知道有多篇文章可以使用Jquery设置默认选项 但我尝试了其中的大部分,但似乎都不起作用 你猜我的情况有什么问题吗 我只需要在页面加载时获取默认选项Javascript 未设置本地存储默认选项的页面加载Jquery,javascript,jquery,Javascript,Jquery,我知道有多篇文章可以使用Jquery设置默认选项 但我尝试了其中的大部分,但似乎都不起作用 你猜我的情况有什么问题吗 我只需要在页面加载时获取默认选项文章类型 代码: <select id="blogarticletype"> <option selected><?php echo $this->__('Article Type') ?></option> <option><?php echo
文章类型
代码:
<select id="blogarticletype">
<option selected><?php echo $this->__('Article Type') ?></option>
<option><?php echo $this->__('All Types') ?></option>
<!--Begin To Retain Article type drop down in local storage after refresh-->
<script type="text/javascript">
if (localStorage.getItem('mySelectLocalstorageValue') === null) {
localStorage.setItem('mySelectLocalstorageValue', "articletype");
//document.write("All Types");
}
else {
if (localStorage.getItem('mySelectLocalstorageValue') === "article") {
document.write("Articles");
} else if (localStorage.getItem('mySelectLocalstorageValue') === "makers") {
document.write("Artists & Makers");
}
</script>
</select>
但当页面加载时,我会得到下拉列表中显示的空值
如果我在inspect for(检查)选项值中手动设置为
selected=“selected”
,则它会显示,但不会在页面加载时显示
$('#blogarticletype [value='+localStorage.getItem("mySelectLocalstorageValue")+']').attr('selected', 'true');
它是有效的。工作演示:
$('#blogarticletype [value='+localStorage.getItem("mySelectLocalstorageValue")+']').attr('selected', 'true');