JS *** 作select下拉框动态变动

JS *** 作select下拉框动态变动,第1张

动态创建select

     function createSelect(){

             var mySelect = document createElement_x("select");

             mySelect id = "mySelect";   

             document body appendChild(mySelect);

     }

添加选项option

   function addOption(){

             //根据id查找对象

               var obj=document getElementByIdx_x( mySelect );

               //添加一个选项

               obj add(new       Option("文本" "值"));     

   }

删除所有选项option

   function removeAll(){

               var obj=document getElementByIdx_x( mySelect );

 

               obj options length= ;   

   }

删除一个选项option

function removeOne(){

               var obj=document getElementByIdx_x( mySelect );

               //index 要删除选项的序号 这里取当前选中选项的序号

               var index=obj selectedIndex;

               obj options remove(index);   

   }

获得选项option的值

var obj=document getElementByIdx_x( mySelect );

var index=obj selectedIndex; //序号 取当前选中选项的序号

var val = obj options[index] value;

获得选项option的文本

var obj=document getElementByIdx_x( mySelect );

var index=obj selectedIndex; //序号 取当前选中选项的序号

var val = obj options[index] text;

修改选项option

var obj=document getElementByIdx_x( mySelect );

var index=obj selectedIndex; //序号 取当前选中选项的序号

var val = obj options[index]=new Option("新文本" "新值");

删除select

     function removeSelect(){

                 var mySelect = document getElementByIdx_x("mySelect");

               mySelect parentNode removeChild(mySelect);

lishixinzhi/Article/program/Java/JSP/201311/20153

页面加载时,因为没有任何选中项,所以 secondElementselectedIndex 值是 -1,secondElement

[-1]为undefined,所以alert()会d出 undefined 或 一个空的对话框;

如果你用鼠标选中了“选项9”,secondElementselectedIndex 值是 0,secondElement

[0]为"<option value="选项9">选项9</option>",所以alert()会d出 [object HTMLOptionElement]

把代码做一些修改:

<option value="选项9">选项9</option> 修改为

<option value="选项9" selected>选项9</option>

alert(secondElement[secondElementselectedIndex]); 修改为 alert(secondElement[secondElementselectedIndex]value);

以上就是关于JS *** 作select下拉框动态变动全部的内容,包括:JS *** 作select下拉框动态变动、javascript获取select标签下的option的value求解、等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/9509109.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-04-29
下一篇2023-04-29

发表评论

登录后才能评论

评论列表(0条)

    保存