checkbox,radio,selected相关操作
1、radio:单选框
HTML代码:
- <input type="radio" name="radio" id="radio1" value="1" />1
- <input type="radio" name="radio" id="radio2" value="2" />2
- <input type="radio" name="radio" id="radio3" value="3" />3
- <input type="radio" name="radio" id="radio4" value="4" />4
js操作代码:
- jQuery("input[type='radio'][name='radio']:checked").length == 0 ? "没有任何单选框被选中" : "已经有选中";
- jQuery('input[type="radio"][name="radio"]:checked').val(); // 获取一组radio被选中项的值
- jQuery("input[type='radio'][name='radio'][value='2']").attr("checked", "checked");// 设置value = 2的一项为选中
- jQuery("#radio2").attr("checked", "checked"); // 设置id=radio2的一项为选中
- jQuery("input[type='radio'][name='radio']").get(1).checked = true; // 设置index = 1,即第二项为当前选中
- var isChecked = jQuery("#radio2").is(":checked");// id=radio2的一项处于选中状态则isChecked = true, 否则isChecked = false;
- var isChecked = jQuery("input[type='radio'][name='radio'][value='2']").is(":checked");// value=2的一项处于选中状态则isChecked = true, 否则isChecked = false;
2、checkbox:复选框
HTML代码:
- <input type="checkbox" name="checkbox" id="checkAll" />全选/取消全选
- <input type="checkbox" name="checkbox" id="checkbox_id1" value="1" />1
- <input type="checkbox" name="checkbox" id="checkbox_id2" value="2" />2
- <input type="checkbox" name="checkbox" id="checkbox_id3" value="3" />3
- <input type="checkbox" name="checkbox" id="checkbox_id4" value="4" />4
- <input type="checkbox" name="checkbox" id="checkbox_id5" value="5" />5
js操作代码:
- var val = jQuery("#checkbox_id1").val();// 获取指定id的复选框的值
- var isSelected = jQuery("#checkbox_id3").attr("checked"); // 判断id=checkbox_id3的那个复选框是否处于选中状态,选中则isSelected=true;否则isSelected=false;
- jQuery("#checkbox_id3").attr("checked", true);// or
- jQuery("#checkbox_id3").attr("checked", 'checked');// 将id=checkbox_id3的那个复选框选中,即打勾
- jQuery("#checkbox_id3").attr("checked", false);// or
- jQuery("#checkbox_id3").attr("checked", '');// 将id=checkbox_id3的那个复选框不选中,即不打勾
- jQuery("input[name=checkbox][value=3]").attr("checked", 'checked');// 将name=checkbox, value=3 的那个复选框选中,即打勾
- jQuery("input[name=checkbox][value=3]").attr("checked", '');// 将name=checkbox, value=3 的那个复选框不选中,即不打勾
- jQuery("input[type=checkbox][name=checkbox]").get(2).checked = true;// 设置index = 2,即第三项为选中状态
- jQuery("input[type=checkbox]:checked").each(function(){ //由于复选框一般选中的是多个,所以可以循环输出选中的值
- alert(jQuery(this).val());
- });
- // 全选/取消全选
- jQuery(function() {
- jQuery("#checkAll").click(function(){
- if(jQuery(this).attr("checked") == true){// 全选
- jQuery("input[type=checkbox][name=checkbox]").each(function(){
- jQuery(this).attr("checked", true);
- });
- } else {// 取消全选
- jQuery("input[type=checkbox][name=checkbox]").each(function(){
- jQuery(this).attr("checked", false);
- });
- }
- });
- });
3、select:下拉框
HTML代码:
- <select name="select" id="select_id" style="width: 100px;">
- <option value="1">11</option>
- <option value="2">22</option>
- <option value="3">33</option>
- <option value="4">44</option>
- <option value="5">55</option>
- <option value="6">66</option>
- </select>
js操作代码:
- /**
- * jQuery获取select的各种值
- */
- jQuery("#select_id").change(function(){ // 1.为Select添加事件,当选择其中一项时触发
- //code...
- });
- var checkValue = jQuery("#select_id").val(); // 2.获取Select选中项的Value
- var checkText = jQuery("#select_id :selected").text(); // 3.获取Select选中项的Text
- var checkIndex = jQuery("#select_id").attr("selectedIndex"); // 4.获取Select选中项的索引值,或者:jQuery("#select_id").get(0).selectedIndex;
- var maxIndex = jQuery("#select_id :last").attr("index"); // 5.获取Select最大的索引值,或者:jQuery("#select_id :last").get(0).index;
- /**
- * jQuery设置Select的选中项
- */
- jQuery("#select_id").get(0).selectedIndex = 1; // 1.设置Select索引值为1的项选中
- jQuery("#select_id").val(4); // 2.设置Select的Value值为4的项选中
- /**
- * jQuery添加/删除Select的Option项
- */
- jQuery("#select_id").append("<option value='新增'>新增option</option>"); // 1.为Select追加一个Option(下拉项)
- jQuery("#select_id").prepend("<option value='请选择'>请选择</option>"); // 2.为Select插入一个Option(第一个位置)
- jQuery("#select_id").get(0).remove(1); // 3.删除Select中索引值为1的Option(第二个)
- jQuery("#select_id :last").remove(); // 4.删除Select中索引值最大Option(最后一个)
- jQuery("#select_id [value='3']").remove(); // 5.删除Select中Value='3'的Option
- jQuery("#select_id").empty(); // 6.清空下拉列表
来自:http://www.cnblogs.com/merlini/archive/2013/05/21/3090165.html
checkbox,radio,selected相关操作的更多相关文章
- checkbox radio 多次操作失效
checkbox radio 多次操作失效 , 将attr替换为prop $(this).attr('checked',true); $(this).attr('checked',false); $( ...
- input 的radio checkbox 和 select 相关操作
1 select 获取和设置值,以及onchange事件 1下拉框option没有checked事件 可通过select 的 onchange事件进行监控,以获取其值 <select name ...
- jquery.validate验证text,checkbox,radio,selected
index.cshtml <form id="formLogin" method="post"> <div> <label for ...
- JQuery对CheckBox的一些相关操作
一.通过选择器选取CheckBox: 1.给CheckBox设置一个id属性,通过id选择器选取: <input type="checkbox" name="myB ...
- 【js】操作checkbox radio 的操作总结
摘要 总是忘记checkbox radio 的具体操作,总是坑自己,总结下记下来 html <input type="checkbox" value="1" ...
- jquery 操作select,checkbox,radio (整理)
在工作中经经常使用到select,checkbox,radio,今天有点空暇就整理一下,免得以后用的时候还要又一次找. 操作select下拉框 -- 获取值或选中项: 1, $("#sele ...
- jquery的radio和checkbox的标签的操作集合
jquery的radio和checkbox的标签的操作集合: $("input[name='radio_name'][checked]").val(); //选择被选中Radio的 ...
- jquery的checkbox,radio,select等方法总结
jquery的checkbox,radio,和select是jquery操作的一个难点和重点,很多前端新手对其了解不是很透彻.时间久了不用,我在写的时候有时也难免对某些操作支支吾吾,记不清楚,现在,对 ...
- jquery 获取和设置 checkbox radio 和 select option的值?
============== 获取和设置 checkbox radio 和 select的值? === val()函数, 其名字就表达了 它的意思: 他就是= value 的简写! val就是valu ...
随机推荐
- EffectiveC#1--尽可能的使用属性(property),而不是数据成员(field)
1.属性可以进行数据绑定 2.可以做数据安全校验.在对数据检测时,如果发现数据不满足条件,最好以抛出异常的形式来解决 如下代码不可取 public string Name { get { if(thi ...
- C# Datatable导出Excel方法
C# 导出Excel方法 先引用下System.IO;System.data; 具体函数如下: public static bool ExportCSV(DataTable dt, string f ...
- 使用Fiddler捕获Java程序中的HTTP请求
默认Java程序是不支持Fiddler获取请求的,因此需要通过设置代理来实现. Fiddler的端口是8888,若Fiddler没有运行,则会抛出异常. HttpClient4.5示例: ...
- Transition 1
W3C标准中对css3的transition这是样描述的:“css的transition允许css的属性值在一定的时间区间内平滑地过渡.这种效果可以在鼠标单击.获得焦点.被点击或对元素任何改变中触发, ...
- 【转】Visual Studio 2010在数据库生成随机测数据
测试在项目中是很重要的一个环节,在Visual Studio 2010中,在测试方面已经有很好的支持了,比如有单元测试,负载测试等等.在数据测试的方面,Visual Studio 2010,还支持对数 ...
- CGRect包含交错,边缘,中心的检测
CGRectContainsPoint函数 判断给定的点是否被一个CGRect包含,可以用CGRectContainsPoint函数 BOOL contains = CGRectCont ...
- busybox中tftp服务器使用命令
参数说明:-l 是local的缩写,后跟存在于Client的源文件名,或下载Client后重命名的文件名.-r 是remote的缩写,后跟Server即PC机tftp服务器根目录中的源文件名,或上传S ...
- c中关于#与##的简易使用
#运算符用于在预编译时,将宏参数转换为字符串 eg. #include <stdio.h>#define CONVERT(f)(#f) void helloworld(){ printf( ...
- (原+转)ubuntu中删除文件夹
转载请注明出处: http://www.cnblogs.com/darkknightzh/p/5638030.html 参考网址: http://zhidao.baidu.com/link?url=A ...
- VIM编辑器操作指令
VIM有三种操作模式: 1,命令模式--command mode 2,输入模式--insert mode 3,底行模式--last line mode [在命令模式的时候,按Shift + :出现的 ...