<body>
<select name="month" id="selMonth" onchange="set()">
<option value="">一月</option>
<option value="">二月</option>
<option value="">三月</option>
<option value="">四月</option>
</select>
<script type="text/javascript">
function set(){
$("#selMonth").text(); //获取select所有文本值
$("#selMonth").find("option:selected").text(); //获取select选中的文本值
$("#selMonth").val(); //获取select选中的value
$("#selMonth")[].selectedIndex; //获取select选中的index
//或$("#selMonth").get(0).selectedIndex;
}
</script>
</body>

一:javascript原生方法

  1:拿到select对象: var  myselect=document.getElementById("test");

  2:拿到选中项的索引:var index=myselect.selectedIndex ;             // selectedIndex代表的是你所选中项的index

  3:拿到选中项options的value:  myselect.options[index].value;

  4:拿到选中项options的text:  myselect.options[index].text;

二:jquery方法

  1:var options=$("#test option:selected");  //获取选中的项

  2:alert(options.val());   //拿到选中项的值

  3:alert(options.text());   //拿到选中项的文本

  jQuery获取Select元素选择的Text和Value:

. $("#select_id").change(function(){//code...});   //为Select添加事件,当选择其中一项时触发
. var checkText=$("#select_id").find("option:selected").text(); //获取Select选择的Text
. var checkValue=$("#select_id").val(); //获取Select选择的Value
. var checkIndex=$("#select_id ").get().selectedIndex; //获取Select选择的索引值
. var maxIndex=$("#select_id option:last").attr("index"); //获取Select最大的索引值

  jQuery获取Select元素,并设置的 Text和Value: 
  1. $("#select_id ").get(0).selectedIndex=1;  //设置Select索引值为1的项选中
  2. $("#select_id ").val(4);   // 设置Select的Value值为4的项选中

    $("#ddlRegType ").attr("value","Normal“);

$("#ddlRegType ").val("Normal");
$("#ddlRegType ").get().value = value;

  3. $("#select_id option[text='jQuery']").attr("selected", true);   //设置Select的Text值为jQuery的项选中,或:

var count=$("#ddlRegType option").length;
for(var i=;i<count;i++){
if($("#ddlRegType ").get().options[i].text == text){
$("#ddlRegType ").get().options[i].selected = true;
break;
}
}

  jQuery添加/删除Select元素的Option项:

. $("#select_id").append("<option value='Value'>Text</option>");  //为Select追加一个Option(下拉项)
. $("#select_id").prepend("<option value='0'>请选择</option>"); //为Select插入一个Option(第一个位置)
. $("#select_id option:last").remove(); //删除Select中索引值最大Option(最后一个)
. $("#select_id option[index='0']").remove(); //删除Select中索引值为0的Option(第一个)
. $("#select_id option[value='3']").remove(); //删除Select中Value='3'的Option
. $("#select_id option[text='4']").remove(); //删除Select中Text='4'的Option

  清空 Select:$("#ddlRegType ").empty();

三、常用的选择的操作:

$("document").ready(function(){
$("#btn1").click(function(){
$("[type='checkbox']").attr("checked",'true');//全选
})
$("#btn2").click(function(){
$("[type='checkbox']").removeAttr("checked");//取消全选
})
$("#btn3").click(function(){
$("[type='checkbox']:even").attr("checked",'true');//选中所有奇数
})
$("#btn4").click(function(){
$("[type='checkbox']").each(function(){//反选
if($(this).attr("checked")){
$(this).removeAttr("checked");
}else{
$(this).attr("checked",'true');
}
})
})
$("#btn5").click(function(){//输出选中的值
var str="";
$("[type='checkbox'][checked]").each(function(){
str+=$(this).val()+"\r\n";
})
alert(str);
})
})

jquery获得select option的值和对select option的操作的更多相关文章

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

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

  2. jquery获得select option的值 和对select option的操作【转藏】

    获取Select : 获取select 选中的 text : $("#ddlRegType").find("option:selected").text(); ...

  3. select 获取选中option的值方法,选中option方法

    options=$("#Select option:selected"); options.attr('name');options.val(); options.text(); ...

  4. Jquery操作select,左右移动,双击移动 取到所有option的值

    $(function () { function MoveItem(fromId, toId) { $("#" + fromId + " option:selected& ...

  5. jQuery获取Radio选择的Value值||两个select之间option的互相添加操作(jquery实现)

    jQuery获取Radio选择的Value值: 1. $("input[name='radio_name'][checked]").val();  //选择被选中Radio的Val ...

  6. jquery获得option的值和对option进行操作

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

  7. jquery获得option的值和对option进行操作 作者: 字体:[增加 减小] 类型:转载 时间:2013-12-13 我要评论

    jquery获得option的值和对option进行操作 作者: 字体:[增加 减小] 类型:转载 时间:2013-12-13我要评论 本文为大家介绍下jquery获得option的值和对option ...

  8. jquery获得option的值(示例)

    jquery获得option的值和对option的操作. jQuery获取Select元素,并选择的Text和Value: 复制代码代码如下: $("#select_id").ch ...

  9. jquery新增,删除 ,修改,清空select中的option

    jQuery获取Select选择的Text和Value: 1. var checkText=jQuery("#select_id").find("option:selec ...

随机推荐

  1. type与instance区别

    class Foo(object): pass class Bar(Foo): pass obj = Bar() # isinstance用于判断,对象是否是指定类或其派生类的实例 print(isi ...

  2. 【BZOJ 3661】 Hungry Rabbit (贪心、优先队列)

    3661: Hungry Rabbit Time Limit: 100 Sec  Memory Limit: 512 MBSec  Special JudgeSubmit: 67  Solved: 4 ...

  3. 使用SoapUI测试windows身份验证的webservice

    有个朋友问到用soapui测试wcf服务时如果使用windows身份验证要怎么传输凭据,于是自己试了一下.其实服务端是wcf还是webservice还是webapi都无所谓,关键是windows身份验 ...

  4. 洛谷.4525.[模板]自适应辛普森法1(Simpson积分)

    题目链接 Simpson积分公式:\[\int_a^bf(x)dx\approx\frac{b-a}{6}\left[f(a)+f(b)+4f(\frac{a+b}{2})\right]\] 推导过程 ...

  5. TortoiseGit + msysgit 记住帐号密码方法及使用密匙的方法

    Windows 重度用户只能用 for windows 的软件了,所以虽然使用 Git,但还是要找专门的 windows 版本. 最近开始使用 GitHub 来托管一些小项目/兴趣,而自己是重度 wi ...

  6. JDK源码学习笔记——Enum枚举使用及原理

    一.为什么使用枚举 什么时候应该使用枚举呢?每当需要一组固定的常量的时候,如一周的天数.一年四季等.或者是在我们编译前就知道其包含的所有值的集合. 利用 public final static 完全可 ...

  7. 【对比分析五】CSS阻塞和JS阻塞

    js 的阻塞特性: 所有浏览器在下载 JS 的时候,会阻止一切其他活动,比如其他资源的下载,内容的呈现等等.直到 JS 下载.解析.执行完毕后才开始继续并行下载其他资源并呈现内容.为了提高用户体验,新 ...

  8. UVALive 4863 Balloons 贪心/费用流

    There will be several test cases in the input. Each test case will begin with a line with three inte ...

  9. ssh 远程链接时出现错误提示:WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED

    ssh 远程链接时出现错误提示 @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ @ WARNING: REMOTE HOST I ...

  10. private、protected、public

      private protected public 本类内 Y Y Y 子类内 N Y Y 外部 N N N <?php class MyClass{ public $public = 'Pu ...