jquery 方法合集
要判断数组中是否包含某个元素
var arr = [ "xml", "html", "css", "js" ]; $.inArray("js", arr); //返回 3,
//全选 $("input[name='checkbox']").attr("checked","true"); //取消全选 $("input[name='checkbox']").removeAttr("checked"); //选中所有基数 $("input[name='checkbox']:even").attr("checked","true"); //选中所有偶数 $("input[name='checkbox']:odd").attr("checked","true"); //反选 $("input[name='checkbox']").each(function(){ if($(this).attr("checked")) {$(this).removeAttr("checked");} else {$(this).attr("checked","true");} //获取选择项的值 $("input[name='checkbox']:checkbox:checked").each(function(){ aa+=$(this).val() }) document.write(aa);
//循环读取checkbox值 $("input[type=checkbox][checked]").each(function(){ //由于复选框一般选中的是多个,所以可以循环输出 alert($(this).val()); });
==================================================================================
prop 和 attr 的使用区别
在高版本的jquery引入prop方法后,什么时候该用prop?什么时候用attr?它们两个之间有什么区别?这些问题就出现了。
关于它们两个的区别,简单总结下:
对于HTML元素本身就带有的固有属性,在处理时,使用prop方法
对于HTML元素我们自己自定义的DOM属性,在处理时,使用attr方法。
<input id="chk1" type="checkbox" />是否可见 <input id="chk2" type="checkbox" checked="checked" my-attr="myAttr" />是否可见
像checkbox,radio和select这样的元素,选中属性对应“checked”和“selected”,这些也属于固有属性,因此需要使用prop方法去操作才能获得正确的结果,my-attr是我自己定义的;咱们使用attr();
$("#chk1").prop("checked") == false $("#chk2").prop("checked") == true $("#chk1").attr("checked") == undefined $("#chk2").attr("checked") == "checked"
==========================================================================================
//注意: 操作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);
二、jquery的checkbox,radio,select等方法总结
// 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; 来判断是否选中!
// radio的jquery日常操作及属性
$("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;
// select下拉框的日常jquery操作
$("#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; } }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
· 25岁的心里话