您好,欢迎来到度乐财经。
搜索
您的当前位置:首页jquery操作selectoption的代码小结_jquery

jquery操作selectoption的代码小结_jquery

来源:度乐财经


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


则可通过以下script代码s来获取选中的value和text
代码如下:
$("#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(){
//添加所需要执行的操作代码
})

Copyright © 2019- dules.cn 版权所有

违法及侵权请联系:TEL:199 18 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务