jquery的checkbox,radio,select
jquery的checkbox,radio,和select是jquery操作的一个难点和重点,很多前端新手对其了解不是很透彻。时间久了不用,我在写的时候有时也难免对某些操作支支吾吾,记不清楚,现在,对其做一些简单的总结!
1、checkbox日常jquery操作。
现在我们以下面的html为例进行checkbox的操作。
<input id="checkAll" type="checkbox" />全选
<input name="subBox" type="checkbox" />项1
<input name="subBox" type="checkbox" />项2
<input name="subBox" type="checkbox" />项3
<input name="subBox" type="checkbox" />项4
全选和全部选代码:
<script type="text/javascript">
$(function() {
$("#checkAll").click(function() {
$('input[name="subBox"]').attr("checked",this.checked);
});
var $subBox = $("input[name='subBox']");
$subBox.click(function(){
$("#checkAll").attr("checked",$subBox.length == $("input[name='subBox']:checked").length ? true : false);
});
});
</script>
checkbox属性:
var val = $("#checkAll").val();// 获取指定id的复选框的值
var isSelected = $("#checkAll").attr("checked"); // 判断id=checkAll的那个复选框是否处于选中状态,选中则isSelected=true;否则isSelected=false; //jquery1.6之后版本,用prop()判断。应该写成
isSelected = $("#checkAll").prop("checked");
$("#checkAll").attr("checked", true);// or
$("#checkAll").attr("checked", 'checked');// 将id=checkbox_id3的那个复选框选中,即打勾
$("#checkAll").attr("checked", false);// or
$("#checkAll").attr("checked", '');// 将id=checkbox_id3的那个复选框不选中,即不打勾
$("input[name=subBox][value=3]").attr("checked", 'checked');// 将name=subBox, value=3 的那个复选框选中,即打勾
$("input[name=subBox][value=3]").attr("checked", '');// 将name=subBox, value=3 的那个复选框不选中,即不打勾
$("input[type=checkbox][name=subBox]").get(2).checked = true;// 设置index = 2,即第三项为选中状态
$("input[type=checkbox]:checked").each(function(){
//由于复选框一般选中的是多个,所以可以循环输出选中的值
alert($(this).val());
});
//jquery1.6之后新增.prop()属性,因此jquery1.6之后的版本,用var isSelected = $("#checkAll").prop(“checked”);选中则isSelected=true;否则isSelected=false; 来判断是否选中!
2、radio的jquery日常操作及属性
我们仍然以下面的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
radio操作如下:
$("input[name=radio]:eq(0)").attr("checked",'checked'); //这样就是第一个选中咯。
//jquery中,radio的选中与否和checkbox是一样的。
$("#radio1").attr("checked","checked");
$("#radio1").removeAttr("checked");
$("input[type='radio'][name='radio']:checked").length == 0 ? "没有任何单选框被选中" : "已经有选中";
$('input[type="radio"][name="radio"]:checked').val(); // 获取一组radio被选中项的值
$("input[type='radio'][name='radio'][value='2']").attr("checked", "checked");// 设置value = 2的一项为选中
$("#radio2").attr("checked", "checked"); // 设置id=radio2的一项为选中
$("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;
3、select下拉框的日常jquery操作
select操作相比checkbox和radio要相对麻烦一些,我们仍然以下面的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>
看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").get(0).index; // 5.获取Select最大的索引值
/**
* jQuery设置Select的选中项
*/
$("#select_id").get(0).selectedIndex = 1; // 1.设置Select索引值为1的项选中
$("#select_id").val(4); // 2.设置Select的Value值为4的项选中
/**
* jQuery添加/删除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();
$("#select_id").find("option:selected").text(); // 获取select 选中的 text :
$("#select_id").val(); // 获取select选中的 value:
$("#select_id").get(0).selectedIndex; // 获取select选中的索引:
//设置select 选中的value:
$("#select_id").attr("value","Normal");
$("#select_id").val("Normal");
$("#select_id").get(0).value = value;
//设置select 选中的text,通常可以在select回填中使用
var numId=33 //设置text==33的选中!
var count=$("#select_id option").length;
for(var i=0;i<count;i++)
{
if($("#select_id").get(0).options[i].text == numId)
{
$("#select_id").get(0).options[i].selected = true;
break;
}
}
1、根据id获取checkbox
$("#Checkbox1");
2、获取所有的checkbox
$("input[type='checkbox']");
//或者
$(":checkbox");
3、获取所有选中的checkbox
$("input:checkbox:checked");
//或
$("input[type='checkbox']:checked");
//或
$("input:[type='checkbox']:checked");
4、获取checkbox值 用.val()获取
$("#Checkbox").val();
5、获取多个选中的checkbox值
var vals = [];
$('input:checkbox:checked').each(function (index, item) {
vals.push($(this).val());
});
6、判断checkbox是否选中(jquery 1.6以前版本 用 $(this).attr(“checked”)
用prop查:选中时checked值为true,未选中时checked值为false
$("#cbCheckbox1").click(function () {
if ($(this).prop("checked")) {
alert("选中");
} else {
alert("没有选中");
}
});
用attr查:选中checked值为checked,未选中时checked值为undefined
7、设置checkbox为选中状态
$('input:checkbox').attr("checked", 'checked');//或$('input:checkbox').prop("checked", true);
8、设置checkbox为不选中状态
$('input:checkbox').prop("checked", false);//或$('input:checkbox').attr("checked", '');
9、设置checkbox为禁用状态(jquery<1.6用attr,jquery>=1.6建议用prop)
$("input[type='checkbox']").attr("disabled", "disabled");
//或
$("input[type='checkbox']").attr("disabled", true);
//或
$("input[type='checkbox']").prop("disabled", true);
//或
$("input[type='checkbox']").prop("disabled", "disabled");
10、设置checkbox为启用状态(jquery<1.6用attr,jquery>=1.6建议用prop)
$("input[type='checkbox']").removeAttr("disabled");
//或
$("input[type='checkbox']").attr("disabled", false);
//或
$("input[type='checkbox']").prop("disabled", "");
//或
$("input[type='checkbox']").prop("disabled", false);
//复制代码
//注意: 操作checkbox的checked,disabled属性时jquery1.6以前版本用attr,1.6以上(包含)建议用prop
//1、根据id获取checkbox
$("#cbCheckbox1");
//2、获取所有的checkbox
$("input[type='checkbox']");//or
$("input[name='cb']");
//3、获取所有选中的checkbox
$("input:checkbox:checked");//or
$("input:[type='checkbox']:checked");//or
$("input[type='checkbox']:checked");//or
$("input:[name='ck']:checked");
//4、获取checkbox值
//用.val()即可,比如:
$("#cbCheckbox1").val();
//5、获取多个选中的checkbox值
var vals = [];
$('input:checkbox:checked').each(function (index, item) {
vals.push($(this).val());
});
//6、判断checkbox是否选中(jquery 1.6以前版本 用 $(this).attr("checked"))
$("#cbCheckbox1").click(function () {
if ($(this).prop("checked")) {
alert("选中");
} else {
alert("没有选中");
}
});
//7、设置checkbox为选中状态
$('input:checkbox').attr("checked", 'checked');//or
$('input:checkbox').attr("checked", true);
//8、设置checkbox为不选中状态
$('input:checkbox').attr("checked", '');//or
$('input:checkbox').attr("checked", false);
//9、设置checkbox为禁用状态(jquery<1.6用attr,jquery>=1.6建议用prop)
$("input[type='checkbox']").attr("disabled", "disabled");//or
$("input[type='checkbox']").attr("disabled", true);//or
$("input[type='checkbox']").prop("disabled", true);//or
$("input[type='checkbox']").prop("disabled", "disabled");
//10、设置checkbox为启用状态(jquery<1.6用attr,jquery>=1.6建议用prop)
$("input[type='checkbox']").removeAttr("disabled");//or
$("input[type='checkbox']").attr("disabled", false);//or
$("input[type='checkbox']").prop("disabled", "");//or
$("input[type='checkbox']").prop("disabled", false);