select下js的基本操作(备忘)

1.增加option:

  function addoption(pa0,pa1,pa2)//select控件,option显示的文本,option的value值

         {
             var option = document.createElement("option");
             option.appendChild(document.createTextNode(pa1));
            option.setAttribute("value",pa2);
            pa0.appendChild(option);
      }

2.获取选中option:

  var obj = document.getElementByIdx_x(”testSelect”); //定位id

  var index = obj.selectedIndex; // 选中索引

  var text = obj.options[index].text; // 选中文本

  var value = obj.options[index].value; // 选中值

3.删除选中option

  var obj=document.getElementById('mySelect');

  var index=obj.selectedIndex;

  obj.options.remove(index);


4.清空select

  mySelect.options.length=0;

转载于:https://my.oschina.net/secyaher/blog/274424

猜你喜欢

转载自blog.csdn.net/weixin_33716941/article/details/91966905