提交表单时option上传所选的下标及基本操作2020-08-17

<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值 


转自:https://www.cnblogs.com/yuer20180726/p/11196488.html


option和select的相关操作:

1、获取选中select的value和text,html

$("#mySelect").val();//获取选中记录的value值$("#mySelect option:selected").text();//获取选中记录的text值

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

varobj = document.getElementById("mySelect");

obj.add(newOption("4","4"));

3、删除所有选项option

varobj = document.getElementById("mySelect");

obj.options.length = 0;

4、删除选中选项option

varobj = document.getElementById("mySelect");varindex = obj.selectedIndex;

obj.options.remove(index);

5、修改选中选项option

varobj = document.getElementById("mySelect");varindex = obj.selectedIndex;

obj.options[index] =newOption("three",3);//更改对应的值obj.options[index].selected =true;//保持选中状态

6、删除select

varobj = document.getElementById("mySelect");

obj.parentNode.removeChild(obj); //移除当前对象

7、select选择的响应事件

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

1.动态创建select

function createSelect(){varmySelect = document.createElement_x("select");

mySelect.id = "mySelect";

document.body.appendChild(mySelect);

}

2.添加选项option

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

3.删除所有选项option

function removeAll(){varobj=document.getElementByIdx_x('mySelect');

obj.options.length=0;

}

4.删除一个选项option

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

obj.options.remove(index);}

5.获得选项option的值

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

6.获得选项option的文本

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

7.修改选项option

varobj=document.getElementByIdx_x('mySelect');varindex=obj.selectedIndex;//序号,取当前选中选项的序号varval = obj.options[index]=newOption("新文本","新值");

8.删除select

function removeSelect(){varmySelect = document.getElementByIdx_x("mySelect");

mySelect.parentNode.removeChild(mySelect);

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。