jQuery获取select值遍历option(添加、移除)

2019-03-2115:53:12WEB前端开发Comments3,685 views字数 2334阅读模式
Excerpt

jquery 获取select值的情况有两种:一种是获得 select 的被选中的那个 option 的 value值,一种是获得 select 的被选中的那个 option 的 innerHTML(即包含在中的内容),删除方法就更简单了直接使用remove,增加直接使用期append即可。

jquery 获取select值的情况有两种:一种是获得 select 的被选中的那个 option 的 value值,一种是获得 select 的被选中的那个 option 的 innerHTML(即包含在中的内容),删除方法就更简单了直接使用remove,增加直接使用期append即可。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/gcs/10213.html

遍历option和添加、移除option文章源自菜鸟学院-https://www.cainiaoxueyuan.com/gcs/10213.html

语法解释:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/gcs/10213.html

代码如下 复制代码文章源自菜鸟学院-https://www.cainiaoxueyuan.com/gcs/10213.html

1. $("#select_id").append("<option value='Value'>Text</option>");  //为Select追加一个Option(下拉项)
2. $("#select_id").prepend("<option value='0'>请选择</option>");  //为Select插入一个Option(第一个位置)
3. $("#select_id option:last").remove();  //删除Select中索引值最大Option(最后一个)
4. $("#select_id option[index='0']").remove();  //删除Select中索引值为0的Option(第一个)
5. $("#select_id option[value='3']").remove();  //删除Select中Value='3'的Option
5. $("#select_id option[text='4']").remove();  //删除Select中Text='4'的Option文章源自菜鸟学院-https://www.cainiaoxueyuan.com/gcs/10213.html

文章源自菜鸟学院-https://www.cainiaoxueyuan.com/gcs/10213.html

代码如下 复制代码文章源自菜鸟学院-https://www.cainiaoxueyuan.com/gcs/10213.html

function changeShipMethod(shipping){
var len = $("select[@name=ISHIPTYPE] option").length
if(shipping.value != "CA"){
$("select[@name=ISHIPTYPE] option").each(function(){
if($(this).val() == 111){
$(this).remove();
}
});
}else{
$("<option value='111'>UPS Ground</option>").appendTo($("select[@name=ISHIPTYPE]"));
}
}文章源自菜鸟学院-https://www.cainiaoxueyuan.com/gcs/10213.html

Select选择的Text和Value:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/gcs/10213.html

$('#testSelect option:selected').text();文章源自菜鸟学院-https://www.cainiaoxueyuan.com/gcs/10213.html

文章源自菜鸟学院-https://www.cainiaoxueyuan.com/gcs/10213.html

$("#testSelect").find('option:selected').text();文章源自菜鸟学院-https://www.cainiaoxueyuan.com/gcs/10213.html

文章源自菜鸟学院-https://www.cainiaoxueyuan.com/gcs/10213.html

$("#testSelect").val();文章源自菜鸟学院-https://www.cainiaoxueyuan.com/gcs/10213.html

语法解释:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/gcs/10213.html

1. $("#select_id").change(function(){//code...});   //为Select添加事件,当选择其中一项时触发
2. var checkText=$("#select_id").find("option:selected").text();  //获取Select选择的Text
3. var checkValue=$("#select_id").val();  //获取Select选择的Value
4. var checkIndex=$("#select_id ").get(0).selectedIndex;  //获取Select选择的索引值
5. var maxIndex=$("#select_id option:last").attr("index");  //获取Select最大的索引值文章源自菜鸟学院-https://www.cainiaoxueyuan.com/gcs/10213.html

jQuery设置Select选择的Text和Value:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/gcs/10213.html

语法解释:
1. $("#select_id ").get(0).selectedIndex=1;  //设置Select索引值为1的项选中
2. $("#select_id ").val(4);   //设置Select的Value值为4的项选中
3. $("#select_id option[text='jQuery']").attr("selected", true);   //设置Select的Text值文章源自菜鸟学院-https://www.cainiaoxueyuan.com/gcs/10213.html


jquery 获取select的值,也就是被选中的那个 option 的 value 属性的值文章源自菜鸟学院-https://www.cainiaoxueyuan.com/gcs/10213.html

//通过文章源自菜鸟学院-https://www.cainiaoxueyuan.com/gcs/10213.html

代码如下 复制代码文章源自菜鸟学院-https://www.cainiaoxueyuan.com/gcs/10213.html

select 的 id
$('#select_id
option:selected').val();
$('#select_id').find('option:selected').val();
//或者用原生的方式
$('#select_id
option:selected')[0].value;
//通过
select 的 name
$('select[name="select_name"]
option:selected').val();
$('select[name="select_name"]').find('option:selected').val();文章源自菜鸟学院-https://www.cainiaoxueyuan.com/gcs/10213.html

例2文章源自菜鸟学院-https://www.cainiaoxueyuan.com/gcs/10213.html

jquery获取select被选中的那个 option 的 innerHTML 值(即text值,也就是在<option></option>中间的内容)文章源自菜鸟学院-https://www.cainiaoxueyuan.com/gcs/10213.html

代码如下 复制代码文章源自菜鸟学院-https://www.cainiaoxueyuan.com/gcs/10213.html

//通过
select 的 id
$('#select_id
option:selected').text();
$('#select_id').find('option:selected').text();
//或者用原生的方式
$('#select_id
option:selected')[0].innerHTML;
//通过
select 的 name
$('select[name="select_name"]
option:selected').text();
$('select[name="select_name"]').find('option:selected').text();文章源自菜鸟学院-https://www.cainiaoxueyuan.com/gcs/10213.html

 文章源自菜鸟学院-https://www.cainiaoxueyuan.com/gcs/10213.html

 文章源自菜鸟学院-https://www.cainiaoxueyuan.com/gcs/10213.html

文章源自菜鸟学院-https://www.cainiaoxueyuan.com/gcs/10213.html
  • 本站内容整理自互联网,仅提供信息存储空间服务,以方便学习之用。如对文章、图片、字体等版权有疑问,请在下方留言,管理员看到后,将第一时间进行处理。
  • 转载请务必保留本文链接:https://www.cainiaoxueyuan.com/gcs/10213.html

Comment

匿名网友 填写信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定