我知道有多个帖子可以使用Jquery设置默认选项。
但我尝试了大部分,但似乎没有起作用。
猜猜我的病情有问题吗?
我只需要在页面加载时获取默认选项Article Type
。
代码:
<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>
页面加载:
jQuery( document ).ready(function() {
jQuery('#blogarticletype option:selected').text();
if (jQuery('#blogarticletype').length) {
jQuery('#blogarticletype').val(localStorage.getItem("mySelectLocalstorageValue"));
}
});
但是当页面加载时,我会在下拉列表中显示空值。
如果我在inspect for选项值中手动设置为selected="selected"
,那么它会显示,但不会显示在页面加载
答案 0 :(得分:0)
此行有变化
$('#blogarticletype [value='+localStorage.getItem("mySelectLocalstorageValue")+']').attr('selected', 'true');