JQuery学习-获取select标签的值

先贴一段我的代码,再细说:

01  $ ( document ). ready ( function ()  {
02      var   checkText ;
03      checkText   =   $ ( 'select#cgo_id' ). find ( "option:selected" ). text ();
04      alert ( checkText );
05      $ ( 'select#cgo_id' ). change (
06          function ()  {
07              checkText   =   $ ( 'select#cgo_id' ). find ( "option:selected" ). text ();
08              alert ( checkText );
09          }
10         )
11  });

var checkText=$("#select_id").find("option:selected").text();  //获取Select选择的Text
var checkValue=$("#select_id").val();  //获取Select选择的Value
var checkIndex=$("#select_id ").get(0).selectedIndex;  //获取Select选择的索引值
var maxIndex=$("#select_id option:last").attr("index");  //获取Select最大的索引值
这两个红色的没使用过。。等我会使用了再修改日志
$("#select_id ").get(0).selectedIndex=1;  //设置Select索引值为1的项选中
这个和上面一样,没用过~
$("#select_id ").val(4);   // 设置Select的Value值为4的项选中
$("#select_id option[text='jQuery']").attr("selected", true);   //设置Select的Text值为jQuery的项选中

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
6. $("#select_id option[text='4']").remove();  //删除Select中Text='4'的Option
这些都比较容易理解。 上面这几行代码参考: http://www.cnblogs.com/yaoshiyou/archive/2010/08/24/1806939.html

下面说下最上面的代码:
$ ( document ). ready()这是jquery的代码,让页面在载入的时候加载

$ ( 'select#cgo_id' ). change()这也是jquery的代码,让select变化的时候响应的函数。






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值