zoukankan      html  css  js  c++  java
  • jQuery操作radio、checkbox、select 集合

    1、单选框

    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);      // 将id=checkbox_id3的那个复选框选中,即打勾

    jQuery("#checkbox_id3").attr("checked", 'checked');    // 将id=checkbox_id3的那个复选框选中,即打勾

    jQuery("#checkbox_id3").attr("checked", false);  // 将id=checkbox_id3的那个复选框不选中,即不打勾

    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(){ 为Select添加事件,当选择其中一项时触发});

    var checkValue = jQuery("#select_id").val(); .获取Select选中项的Value

    var checkText = jQuery("#select_id :selected").text(); 获取Select选中项的Text

    var checkIndex = jQuery("#select_id").attr("selectedIndex"); 获取Select选中项的索引值,或者:jQuery("#select_id").get(0).selectedIndex;

    var maxIndex = jQuery("#select_id :last").attr("index"); 获取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的项选中

    $('#streetid option:contains(' + street + ')').each(function(){ if ($(this).text() == street) { $(this).attr('selected', true); } });

    * 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.清空下拉列表

  • 相关阅读:
    PHP如何判断一个gif图片是否为动画?
    Linux常用系统管理命令(top、free、kill、df)
    Mysql字符串连接函数 CONCAT()与 CONCAT_WS()
    OSChina.net 的 Tomcat 配置 server.xml 参考
    修改Linux默认启动级别或模式
    更改CentOS 6.3 yum源为国内 阿里云源
    PHP session过期机制和配置
    PHP垃圾回收机制防止内存溢出
    memcache与memcached的区别
    【总结】虚拟机VirtualBox各种使用技巧
  • 原文地址:https://www.cnblogs.com/HansZimmer/p/9440815.html
Copyright © 2011-2022 走看看