jQuery操作radio、checkbox、select总结(转载修改)

来源:互联网 发布:淘宝多久自动收货 编辑:程序博客网 时间:2024/05/21 09:28

1、radio:单选框

       HTML代码:

        <input type="radio" name="radio" id="radio1" value='1' /><label for='radio1'>第一个</label>        <input type="radio" name="radio" id="radio2" value='2' /><label for='radio2'>第二个</label>        <input type="radio" name="radio" id="radio3" value='3' /><label for='radio3'>第三个</label>        <input type="radio" name="radio" id="radio4" value='4' /><label for='radio4'>第四个</label>
jquery代码
            $("input[type='radio'][name='radio']:checked").length == 0 ? "没有任何单选框被选中" : "已经有选中";            var r = $("input[type='radio'][name='radio']");            r.change(function () {                var b = $('input[type="radio"][name="radio"]:checked').val(); // 获取一组radio被选中项的值                var b1 = $(this).val();            });            $("input[type='radio'][name='radio'][value='第二个']").attr("checked", "checked"); // 设置value = 第二个的一项为选中                $("#radio3").attr("checked", "checked"); // 设置id=radio3的一项为选中                $("input[type='radio'][name='radio']").get(1).checked = true; // 设置index = 1,即第二项为当前选中                var isChecked = $("#radio2").attr("checked"); // id=radio2的一项处于选中状态则isChecked = true, 否则isChecked = false;                var isChecked = $("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" /><label for='checkAll'>全选/取消全选</label>        <input type="checkbox" name="checkbox" id="checkInvert" /><label for='checkInvert'>反选</label>        <input type="checkbox" name="checkbox" id="checkbox_id1" value="1" /><label for='checkbox_id1'>第一个</label>        <input type="checkbox" name="checkbox" id="checkbox_id2" value="2" /><label for='checkbox_id2'>第二个</label>        <input type="checkbox" name="checkbox" id="checkbox_id3" value="3" /><label for='checkbox_id3'>第三个</label>        <input type="checkbox" name="checkbox" id="checkbox_id4" value="4" /><label for='checkbox_id4'>第四个</label>        <input type="checkbox" name="checkbox" id="checkbox_id5" value="5" /><label for='checkbox_id5'>第五个</label>

jquery代码

        var val = $("#checkbox_id1").val(); // 获取指定id的复选框的值            var isSelected = $("#checkbox_id3").attr("checked"); // 判断id=checkbox_id3的那个复选框是否处于选中状态,选中则isSelected=true;否则isSelected=false;            $("#checkbox_id3").attr("checked", true); // or            $("#checkbox_id3").attr("checked", 'checked'); // 将id=checkbox_id3的那个复选框选中,即打勾            $("#checkbox_id3").attr("checked", false); // or            $("#checkbox_id3").attr("checked", ''); // 将id=checkbox_id3的那个复选框不选中,即不打勾            $("input[name=checkbox][value=3]").attr("checked", 'checked'); // 将name=checkbox, value=3 的那个复选框选中,即打勾            $("input[name=checkbox][value=3]").attr("checked", ''); // 将name=checkbox, value=3 的那个复选框不选中,即不打勾            $("input[type=checkbox][name=checkbox]").get(2).checked = true; // 设置index = 2,即第三项为选中状态            $("input[type=checkbox]:checked").each(function () { //由于复选框一般选中的是多个,所以可以循环输出选中的值                alert($(this).val());        });        $(function () {            // 全选/取消全选            $("#checkAll").click(function () {                if ($(this).attr("checked")) {// 全选                        $("input[type=checkbox][name=checkbox]").each(function () {                        $(this).attr("checked", true);                    });                } else {// 取消全选                        $("input[type=checkbox][name=checkbox]").each(function () {                        $(this).attr("checked", false);                    });                }            });            //反选            $("#checkInvert").click(function () {                $("input[type=checkbox][name=checkbox]").each(function () {                    if ($(this).attr("checked")) {                        $(this).attr("checked", false);                    } else {                        $(this).attr("checked", true);                    }                });            });        });
3、select:下拉框

       HTML代码:

        <select name="select" id="select_id">            <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>
jquery代码

            /**              * jQuery获取select的各种值              */            $("#select_id").change(function () {                         // 1.为Select添加事件,当选择其中一项时触发                     //code...                });            var checkValue = $("#select_id").val();                    // 2.获取Select选中项的Value                var checkText = $("#select_id :selected").text();          // 3.获取Select选中项的Text                 var checkIndex = $("#select_id").attr("selectedIndex");    // 4.获取Select选中项的索引值,或者:$("#select_id").get(0).selectedIndex;                var maxIndex = $("#select_id :last").attr("index");        // 5.获取Select最大的索引值,或者:$("#select_id :last").get(0).index;                /**              * $设置Select的选中项              */            $("#select_id").get(0).selectedIndex = 1;                  // 1.设置Select索引值为1的项选中                $("#select_id").val(4);                                    // 2.设置Select的Value值为4的项选中                /**              * $添加/删除Select的Option项              */            $("#select_id").append("<option value='新增'>新增option</option>");    // 1.为Select追加一个Option(下拉项)                 $("#select_id").prepend("<option value='请选择'>请选择</option>");   // 2.为Select插入一个Option(第一个位置)                $("#select_id").get(0).remove(1);                                      // 3.删除Select中索引值为1的Option(第二个)                $("#select_id :last").remove();                                        // 4.删除Select中索引值最大Option(最后一个)                 $("#select_id [value='3']").remove();                                  // 5.删除Select中Value='3'的Option                 $("#select_id").empty();                                               // 6.清空下拉列表    

0 0