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").attr("checked");// id=radio2的一项处于选中状态则isChecked = true, 否则isChecked = false;
var isChecked = jQuery("input[type='radio'][name='radio'][value='2']").attr("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:下拉框

<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.qingdou.me/5002.html

[转]jQuery操作radio、checkbox、select 集合.的更多相关文章

  1. Jquery操作radio,checkbox,select表单操作实现代码

    一 .Select jQuery获取Select选择的Text和Value: 1. $("#select_id").change(function(){//code...}); / ...

  2. jQuery 操作 radio、select、checkbox

    <script type="text/javascript"> $(function () { 一.radio 1.获取选中值,三种方法都可以: $('input:ra ...

  3. jquery操作radio,checkbox

    1. 获取radio选中的value. $('input:radio[name=sex]:checked').val(); 2. 选择 radio 按钮 (Male). $('input:radio[ ...

  4. jSP的3种方式实现radio ,checkBox,select的默认选择值。

    jSP的3种方式实现radio ,checkBox,select的默认选择值.以radiao 为例:第一种方式:在jsp中使用java 脚本,这个方法最直接,不过脚本太多,不容易维护<%Stri ...

  5. 使用JQUERY操作Radio

    发展中经常使用Radio为了实现用户的选择的影响.我在该项目中使用的一些JQUERY操作Radio该方法.这里分享,对于有需要的朋友参考的. 1.变化radio选择.引发一些结果 $("in ...

  6. 【TP3.2+onethink】radio+checkbox+select 空间 编辑页面选中,附录 js 返回上一页

    1.TP3.2框架 如何实现 [radio+checkbox+select 空间 编辑页面选中],说实话,比较繁琐,不咋地!! 不废话,上代码:(其中 XX_arr  变量一维数组) <div ...

  7. jQuery操作radio、checkbox、select 集合

    1.radio:单选框 HTML代码: <input type="radio" name="radio" id="radio1" va ...

  8. jQuery操作radio、checkbox、select总结

    1.radio:单选框 HTML代码: <input type="radio" name="radio" id="radio1" va ...

  9. jQuery设置radio、select、checkbox只读属性后,如何在后台得到数据

    1 设置表单的readonly属性 对于radio.select.checkbox来说,readonly属性对这三个标签不起什么作用. 2 设置表单的disabled属性 以radio为例说明. 代码 ...

随机推荐

  1. 解决NSTimer存在的内存泄漏的问题

    创建定时器会在一定的间隔后执行某些操作,一般大家会这样创建定时器,这样创建的定时,self对定时器有个引用,定时器对self也有个引用,造成了循环引用,最终造成了内存泄漏,如果定时器在做下载的操作就会 ...

  2. WKWebView浅析

    原文链接:supermokey WKWebView 一个WKWebView对象展示交互的web内容,例如应用于app内的浏览器.你可以在你的App中使用WKWebView. 综述 Important: ...

  3. 解决安装rpm包依赖关系的烦恼 - yum工具介绍及本地源配置方法

    版权声明:本文发布于http://www.cnblogs.com/yumiko/,版权由Yumiko_sunny所有,欢迎转载.转载时,请在文章明显位置注明原文链接.若在未经作者同意的情况下,将本文内 ...

  4. Akka初步介绍

    Akka可能很多人都没有用过,也不知道是什么,但如果说起Scala或Spark就有很多人都听说过或使用过 ,这里简单说下三者的关系Akka是使用Scala开发的,Spark中使用了Akka作为其消息的 ...

  5. 使用EditText的addTextChangedListener(new TextWatcher())方法

    (转:http://www.apkbus.com/android-5257-1-14.html) 在使用EditText的addTextChangedListener(new TextWatcher( ...

  6. Java基础知识笔记(八:集合类)

    目录 1  集合类简介  2  List介绍及简单使用 2.1  LinkedList介绍及简单使用 2.2  ArrayList介绍及简单使用 2.3  Vector介绍及简单使用 2.3.1  S ...

  7. DeprecatedAttribute vs. ObsoleteAttribute

    定义比较 ObsoleteAttribute [SerializableAttribute] [AttributeUsageAttribute(AttributeTargets.Class | Att ...

  8. JS--实现简单轮播(一)

    <!DOCTYPE html><html><head> <title></title> <meta charset=utf-8> ...

  9. uva 11059 maximum product(水题)——yhx

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAB1QAAAMcCAIAAABo0QCJAAAgAElEQVR4nOydW7msuhKF2wIasIAHJK

  10. 如何配置Linux系统的网络IP地址

    一台安装了Linux系统的电脑如果想要联网,首先要做的就是进行网络配置.今天小编就以CentOS6.4系统为例为大家介绍整个网络配置的过程,虽然只是以CentOS6.4系统为例,但是其它的Linux系 ...