最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

JavaScript動態(tài)操作select下拉框

 更新時間:2022年04月10日 09:34:28   作者:離人非淺  
這篇文章介紹了JavaScript動態(tài)操作select下拉框的方法,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下

相信在前端設(shè)計中必然不會少的了表單,因?yàn)榻?jīng)常會使用到下拉框選項,又或是把數(shù)據(jù)動態(tài)回顯到下拉框中。因?yàn)橹盃砍兜給ptgroup標(biāo)簽時遇到了問題,沒查到太過詳細(xì)的解決方案,自己動手操作記錄一下。

首先就是咱們的老朋友"select"標(biāo)簽,因?yàn)樾枰猨s、jq兩種操作,所以就定義兩個select標(biāo)簽。

HTML代碼:

<div style="width: 200px;height: 100px;margin: auto;margin-top: 100px;padding: 20px;background-color: pink;">
	<select id="mySelect1" style="width: 120px;"></select>
	<select id="mySelect2" style="width: 160px;"></select>
	<button id="addSelect2">添加</button> <!-- 此處用于點(diǎn)擊動態(tài)添加到mySelect2 -->
</div>

之后就是引用jq,定義js、jq操作,代碼我都貼下面了。

JS代碼:

<script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js"></script>
<script type="text/javascript">
        //1.動態(tài)操作 - JS方式
        //這里先定義一個json對象,用于獲取并新增到select標(biāo)簽
        let language={
	        "languageList":[
	        {
		    "groupName":"前端",
		    "optionName":[
			    {"languageName":"html"},
			    {"languageName":"CSS"},
			    {"languageName":"javascript"}
				],
	        },
	        {
		    "groupName":"后端",
		    "optionName":[
			    {"languageName":"java"},
			    {"languageName":"JSP"}
				]
	        }
	      ]
	    };
        //language.languageList - 數(shù)據(jù)位置
        let index=0;
        for (obj of language.languageList) {
	        //js創(chuàng)建optgroup標(biāo)簽
	        let optgroup=document.createElement("optgroup");
	        //設(shè)置optgroup標(biāo)簽的label和id值
	        optgroup.label=obj.groupName;
	        optgroup.id="optgroupId"+index;
                //把創(chuàng)建optgroup新增到select下
	        document.getElementById("mySelect1").add(optgroup);
	        //針對optgroup標(biāo)簽,添加它的option標(biāo)簽
	        for (var i = 0; i < obj.optionName.length; i++) {
		        //js創(chuàng)建option標(biāo)簽
		        let option=document.createElement("option");
		        option.value=obj.optionName[i].languageName;
		        option.innerHTML=obj.optionName[i].languageName;
		        document.getElementById("optgroupId"+index).appendChild(option);
	        }
	        index+=1; //自定義下標(biāo)放在最后新增,防止添加option時id增加
        }
		
        //2.動態(tài)新增 - JQ方式
        let item=0;
        $("#addSelect2").click(function(){
	        item=item+1;
	        //jq點(diǎn)擊按鈕后向下拉框新增optgroup標(biāo)簽
	        $("#mySelect2").append("<optgroup id='optgroup"+item+"' label='生成的optgroup標(biāo)簽"+item+"'></optgroup>");
	        let r=Math.floor((Math.random()*5)+1); //生成隨機(jī)數(shù)1-5
                //把隨機(jī)數(shù)個數(shù)個的option添加到當(dāng)前新增的optgroup下
	        for (var i = 1; i <= r; i++) {
		        $("#optgroup"+item).append(`<option value="`+i+`">隨機(jī)生成的option`+i+`</option>`);
	        }
        });
</script>

需要注意的是:盡管用的id是遞增產(chǎn)生的,但前面的名字也不要一樣,我在測試按鈕功能的時候,沒注意就把兩種optgroup的id定義成一樣的,結(jié)果按鈕隨機(jī)生成的option都加到了相應(yīng)id的mySelect1的optgroup里面了。

最后再貼一下運(yùn)行效果

首先就是mySelect1回顯json中的數(shù)據(jù)

點(diǎn)擊添加按鈕,新增到mySelect2

到此這篇關(guān)于JavaScript動態(tài)操作select下拉框的文章就介紹到這了。希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論

无极县| 本溪| 崇礼县| 吴桥县| 五家渠市| 绵阳市| 建德市| 嘉祥县| 张家口市| 广安市| 西林县| 石阡县| 论坛| 定陶县| 十堰市| 行唐县| 曲靖市| 石景山区| 汉阴县| 天柱县| 邹平县| 泾川县| 仁化县| 磐安县| 凉山| 休宁县| 图木舒克市| 英德市| 深水埗区| 新源县| 大悟县| 台东市| 安吉县| 游戏| 如皋市| 南溪县| 历史| 白城市| 铜鼓县| 连州市| 若尔盖县|