html获取下拉框现行选中项,jQuery实现select下拉框获取当前选中文本、值、索引...

//直接保存后缀.htnl用谷歌浏览器打开,亲测有效

$(function(){

//为Select添加事件,当选择其中一项时触发

$("select:eq(0)").change(function(){

//code

});

//获取Select选中的Text:结果是由所有匹配元素包含的文本内容组合起来的文本

var checkText = $("select:eq(0) :selected").text();//建议用这个简单

= $("select:eq(0) option:selected").tetx();

= $("#One").find(":selected").text();

= $("#One").find("option:selected").text();

//如果多选,将返回一个数组,其包含所选的值。

var checkValue = $("#select_id").val();

//获取Select选中匹配元素的当前值,即[即使多选也只]取得第一个匹配元素的val内容

var checkValue = $("select:eq(0) :selected").val();//=========强烈建议用这个,以防多选

//获取Select选中的索引值

var checkIndex = $("#select_id ").get(0).selectedIndex;

//获取Select最大的索引值

var maxIndex = $("#select_id :last").prop("index"); //建议用这个

= $("#select_id option:last").prop("index");

= $("select:eq(0)").find(":last").prop("index")

= $("select:eq(0)").find("option:last").prop("index")

//=========================================================================================

//jQuery设置Select选择的 Text和Value:

// 设置Select的Value值为4的项选中

$("#select_id ").val(4); //用这个

$("#select_id [value='4']").prop("selected", true);

$("#select_id option[value='4']").prop("selected", true);

//设置select中的第一个option被选中

$("select :first").prop("selected", true);//这个

$("select :first").prop("selected", 'selected');

$("select option:first").prop("selected", "true");

$("select option:first").prop("selected", "selected");

//============================================================================================

//jQuery添加/删除Select的Option项

$("#select_id").append("Text"); //为Select末尾追加一个Option(下拉项)

$("#select_id").prepend("请选择"); //为Select首部插入一个Option(第一个位置)

$("#select_id :last").remove(); //删除Select中索引值最大Option(最后一个)

$("#select_id :fist").remove(); //删除Select中索引值最小为0Option(第一个)

$("#select_id [value='3']").remove(); //删除Select中Value='3'的Option

});

苹果

香蕉

草莓

橘子

葡萄

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值