且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何添加“已选择”在使用Javascript或jQuery的选项属性?

更新时间:2023-12-05 12:19:58

localStorage.getItem(Select1)将返回马来西亚和 $(e).val()将在每个循环中返回1,2 ... 5。所以你的情况永远不会成真。而是使用

localStorage.getItem("Select1") will return Malaysia and $(e).val() will return 1,2...5 in each loop. So your condition will never be true. Instead use

<script>
localStorage.setItem("Select1", "4");
    $('#countryselect').find('option').each(function(i,e){
        if($(e).val() == localStorage.getItem("Select1")){
            $('#countryselect').prop('selectedIndex',i);
        }
    });
</script>