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 ...
随机推荐
- 文件系统层级结构标准(FHS)
参考资料:FHS 简介 FHS目前发展到3.0版本,发布于2015年6月3日,由Linux基金会在负责维护.它规定了Linux的文件层级结构,使得各Linux发行版.软件开发商知道应该将哪些文件放在哪 ...
- redis集群错误解决:/usr/lib/ruby/gems/1.8/gems/redis-3.0.0/lib/redis/client.rb:79:in `call': ERR Slot 15495 is already busy (Redis::CommandError)
错误信息: /usr/lib/ruby/gems/1.8/gems/redis-3.0.0/lib/redis/client.rb:79:in `call': ERR Slot 15495 is al ...
- [ 原创 ] Java基础6--构造函数和抽象类的性质
构造函数的性质 // A.方法名与类名相同: // B.没有返回类型(例如return.void等):// C.不能被static.final.native.abstract和synchronized ...
- 社会主义核心价值观js代码
效果如下: 代码如下: <!DOCTYPE html> <html lang="en"> <head> <meta charset=&qu ...
- QQ怎么 发送 已经录好的视频
韩梦飞沙 韩亚飞 313134555@qq.com yue31313 han_meng_fei_sha QQ发送 已经录好的视频 直接放过去,对方是需要下载的. 只有通过QQ录制的,才是直接就 ...
- luogu4770 [NOI2018]你的名字 后缀自动机 + 线段树合并
其实很水的一道题吧.... 题意是:每次给定一个串\(T\)以及\(l, r\),询问有多少个字符串\(s\)满足,\(s\)是\(T\)的子串,但不是\(S[l .. r]\)的子串 统计\(T\) ...
- Educational Codeforces Round 45 (Div 2) (A~G)
目录 Codeforces 990 A.Commentary Boxes B.Micro-World C.Bracket Sequences Concatenation Problem D.Graph ...
- UVALive 4423 String LD 暴力
A - String LD Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Submit Stat ...
- bootstrap字体图标不正常显示的原因
本地引入bootstrap.css文件,使用https://v3.bootcss.com/components/站点 字体图标 时不能正常显示,换成 bootstrap 官网的 cdn 链接却能正常显 ...
- hadoop 2.7.1 高可用安装部署
hadoop集群规划 目标:创建2个NameNode,做高可用,一个NameNode挂掉,另一个能够启动:一个运行Yarn,3台DataNode,3台Zookeeper集群,做高可用. 在hadoop ...