2023年6月21日发(作者:)
jQuery对from表单元素的操作
最近接触jquery⽐较多。其对form的操作跟js还是⼀定区别的。总之jquery功能强⼤,值得⼀学。//遍历option和添加、移除optionfunction changeShipMethod(shipping){var len = $("select[@name=ISHIPTYPE] option").lengthif( != "CA"){$("select[@name=ISHIPTYPE] option").each(function(){if($(this).val() == 111){$(this).remove();}});}else{$("").appendTo($("select[@name=ISHIPTYPE]"));}}//取得下拉選單的選取值$(#testSelect option:selected'').text();或$("#testSelect").find(''option:selected'').text();或$("#testSelect").val();//记 性不好的可以收藏下:1,下拉框:var cc1 = $(".formc select[@name=''country''] option[@selected]").text(); //得到下拉菜单的选中项的⽂本(注意中间有空格)var cc2 = $(''.formc select[@name="country"]'').val(); //得到下拉菜单的选中项的值var cc3 = $(''.formc select[@name="country"]'').attr("id"); //得到下拉菜单的选中项的ID属性值$("#select").empty();//清空下拉框 //$("#select").html('''');$("").appendTo("#select")//添加下拉框的option稍微解释⼀下:[@name=''country''] option[@selected] 表⽰具有name 属性,并 且该属性值为''country'' 的select元素 ⾥⾯的具有selected 属性的option 元素;可以看出有@开头的就表⽰后 ⾯跟的是属性。2,单选框:$("input[@type=radio][@checked]").val(); //得到单选框的选中项的值(注意中间没有空格)$("input[@type=radio][@value=2]").attr("checked",''checked''); //设置单选框value=2的为选中状态.(注意中间没有空格)3,复选框:$("input[@type=checkbox][@checked]").val(); //得到复选框的选中的第⼀项的值$("input[@type=checkbox][@checked]").each(function() { //由于复选框⼀般选中的是多个,所以可以循环输出alert($(this).val());});$("#chk1").attr("checked",'''');//不打勾$("#chk2").attr("checked",true);// 打勾if($("#chk1").attr(''checked'')==undefined){} //判断是否已经打勾当然jquery的选择器是强⼤的. 还有很多⽅法.aaassjquery radio取值,checkbox取值,select取值,radio选中,checkbox选中,select选中,及其相关获取⼀组radio被选中项的值var item = $(''input[@name=items][@checked]'').val();获取select 被选中项的⽂本var item = $("select[@name=items] option[@selected]").text();select 下拉框的第⼆个元素为当前选中值$(''#select_id'')[0].selectedIndex = 1;radio单选组的第 ⼆个元素为当前选中值$(''input[@name=items]'').get(1).checked = true;获取值:⽂ 本框,⽂本区域:$("#txt").attr("value");多选框 checkbox:$("#checkbox_id").attr("value");单选组radio: $("input[@type=radio][@checked]").val();下拉框select: $(''#sel'').val();控制表单元素:⽂本框,⽂本区 域:$("#txt").attr("value",'''');//清空内容$("#txt").attr("value",''11'');// 填充内容多选框checkbox: $("#chk1").attr("checked",'''');//不打勾$("#chk2").attr("checked",true);// 打勾if($("#chk1").attr(''checked'')==undefined) //判断是否已经打勾单选组radio: $("input[@type=radio]").attr("checked",''2'');//设置value=2的项⽬为当前选中项下拉 框select: $("#sel").attr("value",''-sel3'');//设置value=-sel3的项⽬为当前选中项$("
2023年6月21日发(作者:)
jQuery对from表单元素的操作
最近接触jquery⽐较多。其对form的操作跟js还是⼀定区别的。总之jquery功能强⼤,值得⼀学。//遍历option和添加、移除optionfunction changeShipMethod(shipping){var len = $("select[@name=ISHIPTYPE] option").lengthif( != "CA"){$("select[@name=ISHIPTYPE] option").each(function(){if($(this).val() == 111){$(this).remove();}});}else{$("").appendTo($("select[@name=ISHIPTYPE]"));}}//取得下拉選單的選取值$(#testSelect option:selected'').text();或$("#testSelect").find(''option:selected'').text();或$("#testSelect").val();//记 性不好的可以收藏下:1,下拉框:var cc1 = $(".formc select[@name=''country''] option[@selected]").text(); //得到下拉菜单的选中项的⽂本(注意中间有空格)var cc2 = $(''.formc select[@name="country"]'').val(); //得到下拉菜单的选中项的值var cc3 = $(''.formc select[@name="country"]'').attr("id"); //得到下拉菜单的选中项的ID属性值$("#select").empty();//清空下拉框 //$("#select").html('''');$("").appendTo("#select")//添加下拉框的option稍微解释⼀下:[@name=''country''] option[@selected] 表⽰具有name 属性,并 且该属性值为''country'' 的select元素 ⾥⾯的具有selected 属性的option 元素;可以看出有@开头的就表⽰后 ⾯跟的是属性。2,单选框:$("input[@type=radio][@checked]").val(); //得到单选框的选中项的值(注意中间没有空格)$("input[@type=radio][@value=2]").attr("checked",''checked''); //设置单选框value=2的为选中状态.(注意中间没有空格)3,复选框:$("input[@type=checkbox][@checked]").val(); //得到复选框的选中的第⼀项的值$("input[@type=checkbox][@checked]").each(function() { //由于复选框⼀般选中的是多个,所以可以循环输出alert($(this).val());});$("#chk1").attr("checked",'''');//不打勾$("#chk2").attr("checked",true);// 打勾if($("#chk1").attr(''checked'')==undefined){} //判断是否已经打勾当然jquery的选择器是强⼤的. 还有很多⽅法.aaassjquery radio取值,checkbox取值,select取值,radio选中,checkbox选中,select选中,及其相关获取⼀组radio被选中项的值var item = $(''input[@name=items][@checked]'').val();获取select 被选中项的⽂本var item = $("select[@name=items] option[@selected]").text();select 下拉框的第⼆个元素为当前选中值$(''#select_id'')[0].selectedIndex = 1;radio单选组的第 ⼆个元素为当前选中值$(''input[@name=items]'').get(1).checked = true;获取值:⽂ 本框,⽂本区域:$("#txt").attr("value");多选框 checkbox:$("#checkbox_id").attr("value");单选组radio: $("input[@type=radio][@checked]").val();下拉框select: $(''#sel'').val();控制表单元素:⽂本框,⽂本区 域:$("#txt").attr("value",'''');//清空内容$("#txt").attr("value",''11'');// 填充内容多选框checkbox: $("#chk1").attr("checked",'''');//不打勾$("#chk2").attr("checked",true);// 打勾if($("#chk1").attr(''checked'')==undefined) //判断是否已经打勾单选组radio: $("input[@type=radio]").attr("checked",''2'');//设置value=2的项⽬为当前选中项下拉 框select: $("#sel").attr("value",''-sel3'');//设置value=-sel3的项⽬为当前选中项$("
发布评论