jquery获得select option的值和对select option的操作
<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的操作的更多相关文章
- jquery获得select option的值 和对select option的操作
jQuery获取Select元素,并选择的Text和Value: 1. $("#select_id").change(function(){//code...}); //为Se ...
- jquery获得select option的值 和对select option的操作【转藏】
获取Select : 获取select 选中的 text : $("#ddlRegType").find("option:selected").text(); ...
- select 获取选中option的值方法,选中option方法
options=$("#Select option:selected"); options.attr('name');options.val(); options.text(); ...
- Jquery操作select,左右移动,双击移动 取到所有option的值
$(function () { function MoveItem(fromId, toId) { $("#" + fromId + " option:selected& ...
- jQuery获取Radio选择的Value值||两个select之间option的互相添加操作(jquery实现)
jQuery获取Radio选择的Value值: 1. $("input[name='radio_name'][checked]").val(); //选择被选中Radio的Val ...
- jquery获得option的值和对option进行操作
Query获取Select元素,并选择的Text和Value: $("#select_id").change(function(){//code...}); //为Select添加 ...
- jquery获得option的值和对option进行操作 作者: 字体:[增加 减小] 类型:转载 时间:2013-12-13 我要评论
jquery获得option的值和对option进行操作 作者: 字体:[增加 减小] 类型:转载 时间:2013-12-13我要评论 本文为大家介绍下jquery获得option的值和对option ...
- jquery获得option的值(示例)
jquery获得option的值和对option的操作. jQuery获取Select元素,并选择的Text和Value: 复制代码代码如下: $("#select_id").ch ...
- jquery新增,删除 ,修改,清空select中的option
jQuery获取Select选择的Text和Value: 1. var checkText=jQuery("#select_id").find("option:selec ...
随机推荐
- (三)宏 __cplusplus C/C++混合编程
前面一些篇目的内容作为一个在校生,很少用到,可能工作的人会见得多一点,但是第一次整体性的学习还是不希望有落下的东西,虽然不常用但至少要有个印象 那么就进入第三篇<宏 __cplusplus> ...
- [leetcode DP]91. Decode Ways
A message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' - ...
- Ubuntu安装redis和redis-php扩展
通过apt-get安装的redis使用方法 sudo apt-get install redis-server sudo apt-get install php-redis vim /etc/redi ...
- Windows下修改Git bash的HOME路径
Windows中使用http://git-scm.com/安装Git bash工具,默认的HOME和~路径一般都是C:\Users\用户名,每次得用命令切换到常用的Repository下,此操作重复而 ...
- bzoj 3252: 攻略 -- 长链剖分+贪心
3252: 攻略 Time Limit: 10 Sec Memory Limit: 128 MB Description 题目简述:树版[k取方格数] 众所周知,桂木桂马是攻略之神,开启攻略之神 ...
- 【BZOJ-3110】K大数查询 整体二分 + 线段树
3110: [Zjoi2013]K大数查询 Time Limit: 20 Sec Memory Limit: 512 MBSubmit: 6265 Solved: 2060[Submit][Sta ...
- cookie、sessionStorage、localStorage 详解
转自--http://www.cnblogs.com/fly_dragon/p/3946012.html cookie Cookie的大小.格式.存储数据格式等限制,网站应用如果想在浏览器端存储用户的 ...
- CGI 、PHP-CGI、FASTCGI、PHP-FPM
CGI是干嘛的? CGI是为了保证web server传递过来的数据是标准格式的,方便CGI程序的编写者.web server(比如说nginx)只是内容的分发者.比如,如果请求的是/index/ht ...
- 趁热打铁学node
前言 不废话直接官网下载安装. windows安装很简单,双击ok. 完成后命令行输入node -v. 会提示版本号,说明安装成功(Node.js 还自动附带安装了 npm,类似ruby的gem). ...
- GDB 调试PYTHON
http://www.cnblogs.com/dkblog/p/3806277.html