• js 操作select和option常用代码整理


    1、获取选中select的value和text,html代码如下: 

    <select id="mySelect"> 
      <option value="1">one</option> 
      <option value="2">two</option> 
      <option value="3">three</option> 
    </select> 
    $("#mySelect").val(); //获取选中记录的value值 
    $("#mySelect option:selected").text(); //获取选中记录的text值 

    2、运用new Option("文本","值")方法添加选项option 

    var obj = document.getElementById("mySelect");
    obj.add(new Option("4","4"));

    3、删除所有选项option 

    var obj = document.getElementById("mySelect");
    obj.options.length = 0;

    4、删除选中选项option 

    var obj = document.getElementById("mySelect");
    var index = obj.selectedIndex;
    obj.options.remove(index);

    5、修改选中选项option 

    var obj = document.getElementById("mySelect"); 
    var index = obj.selectedIndex; 
    obj.options[index] = new Option("three",3); //更改对应的值 
    obj.options[index].selected = true; //保持选中状态 


    6、删除select 

    var obj = document.getElementById("mySelect"); 
    obj.parentNode.removeChild(obj); //移除当前对象 

    7、select选择的响应事件 

    $("#mySelect").change(function(){ 
      //添加所需要执行的操作代码
    })

      1.动态创建select 

    function createSelect(){ 
      var mySelect = document.createElement_x("select"); 
      mySelect.id = "mySelect"; 
      document.body.appendChild(mySelect); 
    } 

          2.添加选项option 

    function addOption(){ 
      //根据id查找对象, 
      var obj=document.getElementByIdx_x('mySelect'); 
      //添加一个选项 
      obj.add(new Option("文本","值")); //这个只能在IE中有效 
      obj.options.add(new Option("text","value")); //这个兼容IE与firefox 
    } 

            3.删除所有选项option 

    function removeAll(){ 
      var obj=document.getElementByIdx_x('mySelect'); 
      obj.options.length=0; 
    } 

      4.删除一个选项option 

    function removeOne(){ 
      var obj=document.getElementByIdx_x('mySelect'); 
      //index,要删除选项的序号,这里取当前选中选项的序号 
      var index=obj.selectedIndex; 
      obj.options.remove(index); 
    } 

      5.获得选项option的值 

    var obj=document.getElementByIdx_x('mySelect'); 
    var index=obj.selectedIndex; //序号,取当前选中选项的序号 
    var val = obj.options[index].value; 

      6.获得选项option的文本 

    var obj=document.getElementByIdx_x('mySelect'); 
    var index=obj.selectedIndex; //序号,取当前选中选项的序号 
    var val = obj.options[index].text; 

      7.修改选项option 

    var obj=document.getElementByIdx_x('mySelect'); 
    var index=obj.selectedIndex; //序号,取当前选中选项的序号 
    var val = obj.options[index]=new Option("新文本","新值"); 

      8.删除select 

    function removeSelect(){ 
      var mySelect = document.getElementByIdx_x("mySelect"); 
      mySelect.parentNode.removeChild(mySelect); 
    } 
  • 相关阅读:
    你的内存不够啦:c++: internal compiler error: Killed (program cc1plus)
    ARM交叉编译器GNUEABI、NONE-EABI、ARM-EABI、GNUEABIHF等的区别
    arm-linux-gnueabihf、aarch64-linux-gnu等ARM交叉编译GCC的区别
    ubuntu与centos的对比和选择
    Deep Convolutional Network Cascade for Facial Point Detection实践总结
    深度学习(十七)基于改进Coarse-to-fine CNN网络的人脸特征点定位
    《Deep Convolutional Network Cascade for Facial Point Detection》复现
    使用axis2,根据WSDL生成java客户端代码
    oracle字段NCHAR查询,
    【转】fastdfs第一次上传文档报错recv package size
  • 原文地址:https://www.cnblogs.com/mr-wuxiansheng/p/7465852.html
Copyright © 2020-2023  润新知