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 ...
随机推荐
- Bzoj5294/洛谷P4428 [Bjoi2018]二进制(线段树)
题面 Bzoj 洛谷 题解 考虑一个什么样的区间满足重组之后可以变成\(3\)的倍数.不妨设\(tot\)为一个区间内\(1\)的个数.如果\(tot\)是个偶数,则这个区间一定是\(3\)的倍数,接 ...
- Xamarin 2017.11.1更新
Xamarin 2017.11.1更新 本次更新主要解决了一些bug.Visual Studio 2017升级到15.4.2获得新功能.Visual Studio 2015需要工具-选项-Xamar ...
- C#语法文本字面量
C#语法文本字面量 在日常生活中,文本用来表示除了数字以外的内容.例如有一个叫“比尔”的人,他的职位为“科长”.那么,“比尔”和“科长”都可以称为文本.在计算机里,现实世界中的文本通常被称为字符和字符 ...
- 利用arpspoof探取账户密码
---恢复内容开始--- > /proc/sys/net/ipv4/ip_forward 首先在kali里开启IP转发功能 arpspoof -t 被害人ip 网关ip -i eth0 例如 再 ...
- SmartGit 授权文件 Free Trial License to Non-Commercial
To alter the license. First, go to Windows: %APPDATA%\syntevo\SmartGit\ OS X: ~/Library/Preferenc ...
- 【HDU】3401:Trade【单调队列优化DP】
Trade Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submi ...
- BZOJ5217: [Lydsy2017省队十连测]航海舰队 FFT
被FFT的空间卡了半天 后来发现根本不用开那么大... 首先可以把包含舰艇的那个小矩形找出来 将它一行一行连接成一个串T 其中舰艇位置为1其他位置为0 将大矩形也连成串S 其中礁石为1其他为0 两个串 ...
- URAL 1877 Bicycle Codes
1877. Bicycle Codes Time limit: 0.5 secondMemory limit: 64 MB Den has two four-digit combination loc ...
- Codeforces Round #256 (Div. 2) C. Painting Fence
C. Painting Fence Bizon the Champion isn't just attentive, he also is very hardworking. Bizon the Ch ...
- codevs 1204 寻找子串位置 KMP
1204:寻找子串位置 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 18K Solved: 8K Description 给出字符串a和字符串b,保 ...