jQuery的一些方法理出一些常用的方法:

//获取第一个option的值 
$('#test option:first').val(); 
//最后一个option的值 
$('#test option:last').val(); 
//获取第二个option的值 
$('#test option:eq(1)').val(); 
//获取选中的值 
$('#test').val(); 
$('#test option:selected').val(); 
//设置值为2的option为选中状态 
$('#test').attr('value','2'); 
//设置第一个option为选中 
$('#test option:last').attr('selected','selected'); 
$("#test").attr('value' , $('#test option:last').val()); 
$("#test").attr('value' , $('#test option').eq($('#test option').length - 1).val()); 
//获取select的长度 
$('#test option').length; 
//添加一个option 
$("#test").append("ff"); 
$("ff").appendTo("#test"); 
//添除选中项 
$('#test option:selected').remove(); 
//指定项选中 
$('#test option:first').remove(); 
//指定值被删除 
$('#test option').each(function(){ 
if( $(this).val() == '5'){ 
$(this).remove(); 

}); 
$('#test option[value=5]').remove();

//获取第一个Group的标签 
$('#test optgroup:eq(0)').attr('label'); 
//获取第二group下面第一个option的值 
$('#test optgroup:eq(1) :option:eq(0)').val();

获取select中选择的text与value相关的值

获取select选择的Text : var checkText=$("#slc1").find("option:selected").text(); 
获取select选择的value:var checkValue=$("#slc1").val(); 
获取select选择的索引值: var checkIndex=$("#slc1 ").get(0).selectedIndex; 
获取select最大的索引值: var maxIndex=$("#slc1 option:last").attr("index");

设置select选择的Text和Value

设置select索引值为1的项选中:$("#slc1 ").get(0).selectedIndex=1; 
设置select的value值为4的项选中: $("#slc1 ").val(4); 
设置select的Text值为JQuery的选中: 
$("#slc1 option[text='jQuery']").attr("selected", true); 
PS:特别要注意一下第三项的使用哦。看看JQuery的选择器功能是如此地强大呀!

添加删除option项

为select追加一个Option(下拉项) 
$("#slc2").append(""+i+""); 
为select插入一个option(第一个位置) 
$("#slc2").prepend("请选择"); 
PS: prepend 这是向所有匹配元素内部的开始处插入内容的最佳方式。 
删除select中索引值最大option(最后一个) 
$("#slc2 option:last").remove(); 
删除select中索引值为0的option(第一个) 
$("#slc2 option[index='0']").remove(); 
删除select中value='3'的option 
$("#slc2 option[value='3']").remove(); 
删除select中text='4'的option 
$("#slc2 option[text='3']").remove();

jQuery获取select option的更多相关文章

  1. Jquery获取select option动态添加自定义属性值失效

    Jquery获取select option动态添加自定义属性值失效 2014/12/31 11:49:19 中国学网转载 编辑:李强 http://www.xue163.com/588880/3909 ...

  2. 【转】jQuery获取Select option 选择的Text和Value

    获取一组radio被选中项的值:var item = $('input[name=items][checked]').val();获取select被选中项的文本:var item = $(" ...

  3. jquery 获取Select option 选择的Text和Value

    jquery radio取值,checkbox取值,select取值,radio选中,checkbox选中,select选中,及其相关设置 获取一组radio被选中项的值:var item = $(' ...

  4. jQuery获取Select option 选择的Text和 Value

    获取一组radio被选中项的值:var item = $('input[name=items][checked]').val();获取select被选中项的文本var item = $("s ...

  5. 通过JQUERY获取SELECT OPTION中选中的值

    遇到一样学一样. 一个是取KEY,一个是取VALUE,一个是取所有文本. var dbuser_select = $("#dbuser_select option:selected" ...

  6. Jquery获取select选中的文本与值

    jquery获取select选择的文本与值获取select :获取select 选中的 text :    $("#ddlregtype").find("option:s ...

  7. jquery获得select option的值 和对select option的操作

    jQuery获取Select元素,并选择的Text和Value: 1. $("#select_id").change(function(){//code...});   //为Se ...

  8. Jquery获取select,dropdownlist,checkbox下拉列表框的值

       jQuery获取 Select选择的Text和Value: 语法解释: 1. $("#select_id").change(function(){//code...});   ...

  9. jQuery获取Select选择的Text和 Value(转)用时比较方便寻找

    ---恢复内容开始--- jQuery获取Select选择的Text和Value:语法解释:1. $("#select_id").change(function(){//code. ...

随机推荐

  1. (原)Mac下Apache添加限制IP线程模块:mod_limitipconn.so

    1.下载:http://dominia.org/djao/limit/mod_limitipconn-0.24.tar.bz2 解压到一个目录:我的是~/Download/mod_limitipcon ...

  2. 【Android】还原“微信”apk中的“发现”和“我”两个模块

    先下载一个微信apk,以压缩包的形式打开,对比微信界面,找出我们需要的素材. 以下两个模块的还原完全采用RelativeLayout相对布局. 按钮效果的实现 点击对应版块,将有点击效果.这可以通过修 ...

  3. 通过代码设置radiobutton不同方位图标的两种方法

    更换radiobutton中的图片在xml中很好设置,但对于初学者如何在代码中设置还是不容易找的.没法子,通过看原版api找到两个方法,setCompoundDrawables和setCompound ...

  4. SOA——面向服务的体系架构

    上一篇博文中提到了"紧耦合"的现象.怎样解决?SOA.採用面向服务的体系架构. 一.What? SOA=Service-oriented Architecture面向服务的体系结构 ...

  5. C语言union关键字

    union 关键字的用法与struct 的用法非常类似. union 维护足够的空间来置放多个数据成员中的“一种”,而不是为每一个数据成员配置空间,在union 中所有的数据成员共用一个空间,同一时间 ...

  6. taglib的使用

    使用自定义的taglib可以是我们对页面数据的处理放在后台,不仅使用方便,而且影藏了处理逻辑,也更加的安全. 需要使用到servlet.jar 1.在web-inf下建立taglib.tld文件 &l ...

  7. C#类中字段,属性与方法

    person类 using System; using System.Collections.Generic; using System.Linq; using System.Text; using ...

  8. django TypeError: 'module' object is not callable

    原因:导入模块时直接把模块当函数使用 from rest_framework import reverse #import reverse module @api_view(("GET&qu ...

  9. 深入研究MiniMVC之后续篇

    今天在园子看到<深入研究 蒋金楠(Artech)老师的 MiniMvc(迷你 MVC),看看 MVC 内部到底是如何运行的>之后,本来是不打算开博来续这个后传,不过,在那边回了个评论之后, ...

  10. php生成批次唯一code(转)

    /** * @param int $no_of_codes//定义一个int类型的参数 用来确定生成多少个优惠码 * @param array $exclude_codes_array//定义一个ex ...