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. facebook分享遇到的错误解决方法

    *** Terminating app due to uncaught exception 'InvalidOperationException', reason: ''App ID not foun ...

  2. 设计模式16---设计模式之组合模式(Composite)(行为型)

    1.场景模拟 使用软件模拟大树的根节点和树枝节点和叶子节点 抽象为两类,容器节点和叶子节点 2.不用模式的解决方案 package demo14.composite.example1; import ...

  3. 共享IP云主机(VPS)玩转wdcp

    目前国内有不少性能还不错的共享IP VPS,但因为没有独立IP,所以环境配置起来会比较麻烦. 因为本人自己现在用的就是共享IP的vps,所以把一些配置方法分享一下,供大家参考. 首先是系统的选择,根据 ...

  4. 2013国内IT行业薪资对照表【技术岗】

    (本文为转载,具体出处不详) 说薪水,是所有人最关心的问题.我只 想说如果想在薪水上面满意,在中国,没有哪里比垄断国企好.电力.烟草.通信才是应该努力的方向.但是像我们这种搞研发的进IT行业似乎是注定 ...

  5. Python笔记:使用pywin32处理excel文件

    因为前端同事须要批量的对excel文件做特殊处理,删除指定行,故写了此脚本.同一时候配合config.ini方便不熟悉py的同事使用 #!/usr/bin/env python #-*- coding ...

  6. 使用VisualVM查看Java Heap Dump

    浏览Heap Dump 可以使用VisualVM浏览heap dump文件的内容,从而快速查看在堆中分配的对象.Heap dumps在主窗口的heap dump子标签页中显示.你可以打开保存在本地的h ...

  7. CentOS7--64安装python的psutil模块

    1.以root身份登陆CentOS依次 执行以下命令: wget https://pypi.python.org/packages/source/p/psutil/psutil-2.1.3.tar.g ...

  8. 从html字符串中获取div内容---jquery

    思考的问题: 怎么在一个网页的div中嵌套另外的网页(不使用inclue,iframe和frame,不使用他们的原因,include只能嵌套静态网页,iframe对网络爬虫影响,frame嵌套网页无法 ...

  9. Swift - 43 - 继承, 多态, 析构函数

    import Foundation /* 什么叫继承: 可以简单理解为一个类可以从它的父类或者基类中直接拿属性或者方法去使用 冒号":"表示两者之间的继承关系 */ class P ...

  10. SqlCommand.Parameters.add()方法

    SqlParameter 类 表示 SqlCommand 的参数,也可以是它到 DataSet 列的映射.无法继承此类. 命名空间:  System.Data.SqlClient 程序集:  Syst ...