jquery取radio单选按钮的值

合集下载

Jquery操作单选按钮(Radio)的取值赋值实现代码

Jquery操作单选按钮(Radio)的取值赋值实现代码

Jquery操作单选按钮(Radio)的取值赋值实现代码1.获取选中值,三种⽅法都可以:01. $('input:radio:checked').val();02. $("input[type='radio']:checked").val();03. $("input[name='rd']:checked").val();2、设置第⼀个Radio为选中值:01. $('input:radio:first').attr('checked', 'checked');02. 或者03. $('input:radio:first').attr('checked', 'true');04. 注: attr("checked",'checked')= attr("checked", 'true')= attr("checked", true)3.设置最后⼀个Radio为选中值:01. $('input:radio:last').attr('checked', 'checked');02. 或者03. $('input:radio:last').attr('checked', 'true');4.根据索引值设置任意⼀个radio为选中值:01. $('input:radio').eq(索引值).attr('checked', 'true');索引值=0,1,2....02. 或者03. $('input:radio').slice(1,2).attr('checked', 'true');5.根据Value值设置Radio为选中值01. $("input:radio[value='rd2']").attr('checked','true');02. 或者03. $("input[value='rd2']").attr('checked','true');6.删除Value值为rd2的Radio01. $("input:radio[value='rd2']").remove();7.删除第⼏个Radio01. $("input:radio").eq(索引值).remove();索引值=0,1,2....02. 如删除第3个Radio:$("input:radio").eq(2).remove();8.遍历Radio01. $('input:radio').each(function(index,domEle){02. //写⼊代码03. });。

Jquery操作Html控件CheckBox、Radio、Select控件

Jquery操作Html控件CheckBox、Radio、Select控件

Jquery操作Html控件CheckBox、Radio、Select控件在使⽤ Javascript 编写前台脚本的时候,经常会操作 Html 控件,⽐如 checkbox、radio、select,⽤ Jquery 库操作其他会⽅便很多,下⾯⽤Jq对这些控件的操作进⾏⼀个全⾯的代码总结。

⼀、Jquery 对 CheckBox 的操作:<input id="ckb1" name="ckb" checked="checked" value="0" type="checkbox"/><span>篮球</span><input id="ckb2" name="ckb" checked="checked" value="1" type="checkbox"/><span>排球</span><input id="ckb3" name="ckb" disabled="disabled" value="2" type="checkbox"/><span>乒乓球</span><input id="ckb4" name="ckb" disabled="disabled" value="3" type="checkbox"/><span>⽻⽑球</span>1、查找控件:(1) 选择所有的 checkbox 控件:根据input类型选择: $("input[type=checkbox]") 等同于⽂档中的 $("input:checkbox")根据名称选择:$("input[name=ckb]")(2) 根据索引获取checkbox控件:$("input:checkbox:eq(1)") 结果返回:<input id="ckb2" name="ckb" value="1" type="checkbox" /><span>排球</span>(3) 获得所有禁⽤的 checkbox 控件:$("input[type=checkbox]:disabled")结果返回:<input id="ckb3" name="ckb" disabled="disabled" value="2" type="checkbox" /><span>乒乓球</span><input id="ckb4" name="ckb" disabled="disabled" value="3" type="checkbox" /><span>⽻⽑球</span>(4)获得所有启⽤的checkbox控件$("input:checkbox[disabled=false]")结果返回:<input id="ckb1" name="ckb" checked="checked" value="0" type="checkbox" /><span>篮球</span><input id="ckb2" name="ckb" checked="checked" value="1" type="checkbox" /><span>排球</span>(5)获得所有checked的checkbox控件$("input:checkbox:checked")结果返回:<input id="ckb1" name="ckb" checked="checked" value="0" type="checkbox" /><span>篮球</span><input id="ckb2" name="ckb" checked="checked" value="1" type="checkbox" /><span>排球</span>(6)获取所有未checkd的checkbox控件$("input:checkbox:[checked=false]")结果返回:<input id="ckb3" name="ckb" disabled="disabled" value="2" type="checkbox" /><span>乒乓球</span><input id="ckb4" name="ckb" disabled="disabled" value="3" type="checkbox" /><span>⽻⽑球</span>(7)获得value 为 0 的checkbox 控件$("input[type=checkbox][value=0]")结果返回:<input id="ckb1" name="ckb" checked="checked" value="0" type="checkbox" /><span>篮球</span>2、禁⽤:(1)禁⽤所有的checkbox控件:$("input:checkbox").attr("disabled", true)(2)启⽤某些禁⽤的 checkbox 控件:$("input:checkbox:disabled").attr("disabled", false);(3)判断value=0的checkbox是否禁⽤:if ($("input[name=ckb][value=0]").attr("disabled") == true) {alert("不可⽤");}else {alert("可⽤");}3、选择:(1)全选:$("input:checkbox").attr("checked", true);(2)全不选:$("input:checkbox").attr("checked", false);(3)反选:$("input:checkbox").each(function () {if ($(this).attr("checked")) {//$(this).removeAttr("checked");$(this).attr("checked", false);}else {$(this).attr("checked",true);}});4、取值:function GetCkboxValues() {var str="";$("input:checkbox:checked").each(function () {switch ($(this).val()) {case "0":str += "篮球,";break;case "1":str += "排球,"; break;case "2":str += "乒乓球,";break;case "3":str += "⽻⽑球,";break;}});str=str.substring(0, str.length - 1)}⼆、Jquery 对 Radio 的操作:<input name="edu" value="0" type="radio" checked="checked"/><span>专科</span><input name="edu" value="1" type="radio"/><span>本科</span><input name="edu" value="2" type="radio" disabled="disabled"/><span>研究⽣</span> <input name="edu" value="3" type="radio" disabled="disabled"/><span>博⼠⽣</span> 1、查找控件:(1)选择所有的 Radio控件//根据input类型选择$("input[type=radio]") //等同于⽂档中的 $("input:radio")//根据名称选择$("input[name=edu]")(2)根据索引获得 Radio控件$("input:radio:eq(1)")结果返回:<input name="edu" value="1" type="radio" /><span>本科</span>(3)获得所有禁⽤的 Radio 控件$("input:radio:disabled")结果返回:<input name="edu" value="2" type="radio" disabled="disabled" /><span>研究⽣</span><input name="edu" value="3" type="radio" disabled="disabled"/><span>博⼠⽣</span> (4)获得所有启⽤的 Radio 控件$("input:radio[disabled=false]")结果返回:<input name="edu" value="0" type="radio" checked="checked" /><span>专科</span><input name="edu" value="1" type="radio" /><span>本科</span>(4)获得checked的 RadioButton 控件$("input:radio:checked") //等同于 $("input[type=radio][checked]")结果返回:<input name="edu" value="0" type="radio" checked="checked" /><span>专科</span>(5)获取未checked的 RadioButton 控件$("input:radio[checked=false]").attr("disabled", true);结果返回:<input name="edu" value="1" type="radio" /><span>本科</span><input name="edu" value="2" type="radio" disabled="disabled" /><span>研究⽣</span><input name="edu" value="3" type="radio" disabled="disabled"/><span>博⼠⽣</span> (6)获得value 为 0 RadioButton 控件$("input[type=radio][value=0]")结果返回:<input name="edu" value="0" type="radio" checked="checked" /><span>专科</span>2、禁⽤:(1)禁⽤所有的Radio$("input:radio").attr("disabled", true);或者 $("input[name=edu]").attr("disabled", true);(2)禁⽤索引为1的Radio控件$("input:radio:eq(1)").attr("disabled", true);(3)启⽤禁⽤的Radio控件$("input:radio:disabled").attr("disabled", false);(4)禁⽤当前已经启⽤的Radio控件$("input:radio[disabled=false]").attr("disabled", true);(5)禁⽤ checked 的RadioButton控件$("input[type=radio][checked]").attr("disabled", true);(6)禁⽤未checked 的RadioButton控件$("input:[type=radio][checked=false]").attr("disabled", true);(7)禁⽤value=0 的RadioButton$("input[type=radio][value=0]").attr("disabled", true);3、取值:$("input:radio:checked").val()4、选择:(1)判断value=1 的radio控件是否选中,未选中则选中:var v = $("input:radio[value=1]").attr("checked"); if (!v) { $("input:radio[value=1]").attr("checked", true); } (2)转换成Dom元素数组来进⾏控制选中:$("input:radio[name=edu]").get(1).checked = true;三、Jquery 对 Select 操作<select id="cmbxGame"><option value="0" selected="selected">⿊猫警长</option><option value="1" disabled="disabled">⼤头⼉⼦</option><option value="2">熊出没</option><option value="3">喜⽺⽺</option></select>1、禁⽤:(1)禁⽤select 控件$("select").attr("disabled", true);(2)禁⽤select中所有option$("select option").attr("disabled", true);(3)禁⽤value=2 的option$("select option[value=2]").attr("disabled", true);(4)启⽤被禁⽤的option$("select option:disabled").attr("disabled", false);2、选择:(1)option 值为 2 的被选择:var v = $("select option[value=2]").attr("selected");if (!v) {$("select option[value=2]").attr("selected", true);}(2) 索引为 2 的option 项被选择$("select")[0].selectedIndex = 2;或者 $("select").get(0).selectedIndex = 2;或者 $("select option[index=2]").attr("selected", true);3、获取选择项的索引:(1)获取选中项索引:jq 中的 get 函数是将jq对象转换成了dom元素var selectIndex = $("select").get(0).selectedIndex;或者 var selectIndex = $("select option:selected").attr("index");(2)获取最⼤项的索引:var maxIndex = $("select option:last").attr("index")或者 var maxIndex = $("select option").length - 14、删除select 控件中的option(1)清空所有option$("select option").empty();(2)删除 value=2 的option$("select option[value=2]").remove();(3)删除第⼀个option$("select option[index=0]").remove();(4)删除 text="熊出没" 的option$("select option[text=熊出没]").remove(); //此⽅法某些浏览器不⽀持⽤下⾯的⽅法替代注意:each 中不能⽤break ⽤return false 代替,continue ⽤ return true 代替$("select option").each(function () {if ($(this).text() == "熊出没") {$(this).remove();return false;}});5、在select中插⼊option(1)在⾸位置插⼊ option 并选择$("select").prepend("<option value='0'>请选择</option>");$("select option[index=0]").attr("selected", true);(2)在尾位置插⼊ option 并选择$("select").append("<option value=\"5\">哪吒闹海</option>");var maxIndex = $("select option:last").attr("index")$("select option[index=" + maxIndex + "]").attr("selected", true);(3)在固定位置插⼊⽐如第⼀个option 项之后插⼊新的option 并选择$("<option value=\"5\">哪吒闹海</option>").insertAfter("select option[index=0]");或者$("select option[index=0]").after("<option value=\"5\">哪吒闹海</option>"); $("select option[index=1]").attr("selected", true);6、取值:function GetCbxSelected() {var v = $("select option:selected").val();var t = $("select option:selected").text();alert("值:" + v + "⽂本:" + t);}。

单选框radio改变事件详解(用的jquery的radio的change事件)

单选框radio改变事件详解(用的jquery的radio的change事件)

单选框radio改变事件详解(⽤的jquery的radio的change事件)单选框radio改变事件详解(⽤的jquery的radio的change事件)⼀、总结1、⽤的jquery的radio的change事件:当元素的值发⽣改变时,会发⽣ change 事件,radio选择不同选项的时候恰巧是值发⽣改变。

⼆、单选框radio改变事件详解<input type="radio" name="bedStatus" id="allot" checked="checked" value="allot">Allot<input type="radio" name="bedStatus" id="transfer" value="transfer">Transfer1 $(document).ready(function() {2 $('input[type=radio][name=bedStatus]').change(function() {3if (this.value == 'allot') {4 alert("Allot Thai Gayo Bhai");5 }6else if (this.value == 'transfer') {7 alert("Transfer Thai Gayo");8 }9 });10 });三、单选框选择不同的选项登录的账号密码⾃动改变1<form class="am-form tpl-form-line-form" action="" method="post">23<div class="am-form-group">4<label class="am-radio-inline tpl-login-remember-me">5<input class="tpl-form-input" type="radio" name="status" id="student" value="0" checked="checked">Student6</label>7<label class="am-radio-inline tpl-login-remember-me">8<input class="tpl-form-input" type="radio" name="status" id=teacher value="1">Teacher9</label>10</div>1112<div class="am-form-group">13<input type="text" class="tpl-form-input" id="username" name="username" required="" value="" placeholder="username">1415</div>1617<div class="am-form-group">18<input type="password" class="tpl-form-input" id="password" name="password" required="" value="" placeholder="password">1920</div>2122<!-- 验证码 -->23<!-- <div class="am-form-group">24 <input type="text" class="tpl-form-input" id="user-name" name="code" placeholder="CAPTCHA">25 </div>26 <div class="am-form-group">27 <img width="100%" style="cursor: pointer" src="{:captcha_src()}" alt="captcha" onclick="this.src='{:captcha_src()}?'+Math.random();" />28 </div> -->29<!--End 验证码 -->303132<div class="am-form-group tpl-login-remember-me">33<input id="remember-me" type="checkbox">34<label for="remember-me">3536记住密码37</label>38<label style="margin-left: 15px">39<a href="{:url('login/register')}">注册</a>40</label>4142</div>4344<div class="am-form-group">4546<button type="submit" class="am-btn am-btn-primary am-btn-block tpl-btn-bg-color-success tpl-login-btn">提交</button> 4748</div>49</form>js1 <script>2 $(document).ready(function() {3 $('input[type=radio][name=status]').change(function() {4if (this.value == '0') {5 $("#username").val("student");6 $("#password").val("student");7 }8else if (this.value == '1') {9 $("#username").val("teacher");10 $("#password").val("teacher");11 }12 });13 });14 </script>。

jquery】常用的jquery获取表单对象的属性与值

jquery】常用的jquery获取表单对象的属性与值

jquery】常⽤的jquery获取表单对象的属性与值1【jquery】常⽤的jquery获取表单对象的属性与值234 1、JQuery的概念56789 JQuery是⼀个JavaScript的类库,这个类库集合了很多功能⽅法,利⽤类库你可以⽤⼀些简单的代码实现⼀些复杂的JS效果。

101112 2、JQuery实现了代码的分离1314不⽤再⽹页中加⼊如:onclick之类的事件来调⽤函数了,直接引⼊JQuery类库和⾃⼰编写的JQuery代码就可以了;15如:16 $(function(){17 $("Element").click{function(){18 alert("点击我哦!");19 }20 }21 });22上⾯的代码中只要定义了Element 这个元素后⾯的click是动作2324 alert("点击我哦!");这个是要执⾏的代码,当然你可以有很多的操作在这个函数中;25这⾥⾯的$这个号代表JQuery的意思,就是引⽤类库。

2627 3、JQuery的核⼼的⼀些⽅法28 each(callback) '就像循环29 $("Element").length; ‘元素的个数,是个属性30 $("Element").size(); ’也是元素的个数,不过带括号是个⽅法31 $("Element").get(); ‘某个元素在页⾯中的集合,以数组的形式存储32 $("Element").get(index); ’功能和上⾯的相同,index表⽰第⼏个元素,数组的下标33 $("Element").get().reverse(); ‘把得到的数组⽅向34 ("Element1").index(("Element1").index(("Element2")); ’元素2在元素1中的索引值是。

单选按钮(radio)的取值和点击事件

单选按钮(radio)的取值和点击事件

单选按钮(radio)的取值和点击事件笔记⾛⼀波:获取单选按钮(radio)的选中值,以及它的点击事件的实现⾸先要引⼊Jquery<script type="text/javascript" src="js/jquery-3.1.1.min.js">下⾯是⼀个简单的表单<!-- 单选按钮的取值和点击事件--><form action="#" method="post">性别:<input type="radio" name="sex" value="male" checked="true"/>男<input type="radio" name="sex" value="female"/>⼥<input type="button" id="getSexBtn" value="获取性别"/></form>就这么丑啊!就这么丑!需求⼀:点击“获取性别”按钮,使⽤alert()弹出选中按钮的value值// 通过type="radio"获取选中的值$("#getSexBtn").click(function(){var sex = $("input[type=radio]:checked").val();alert(sex);});// 通过name="sex"获取选中的值$("#getSexBtn").click(function(){var sex = $("input[name=sex]:checked").val();alert(sex);});需求⼆:选中“男”或者“⼥”时,弹出选中的按钮的value值,即按钮的点击事件// 点击单选按钮后触发,即,我们选择“男”时,触发⼀个事件,弹出选中的值$("input[name=sex]").click(function(){var sex = $(this).val();alert(sex);});不积跬步,⽆以⾄千⾥。

jQuery获取Radio选择的Value值

jQuery获取Radio选择的Value值
<input type="checkbox" name="chk_all" id="chk_all" />全选/取消全选
<script type="text/javascript">
$("#chk_all").click(function() { $("input[name='chk_list']").attr("checked",$(this).attr("checked"));});
for (var i=0;i<arrChk.length;i++){ alert(arrChk[i].value); }
</script>
<script type="text/javascript">
var arrChk=$("input[name='chk_list']:checked"); $(arrChk).each(function() { window.alert(this.value); }); });</script>
3. $("#select_id option[text='jQuery']").attr("selected", true); //设置Select的Text值为jQuery的项选中
jQuery添加/删除Select的Option项:
点击一次,Select将追加一个Option
点击将在Select第一个位置插入一个Option

jquery获取radio值(单选组radio)

jquery获取radio值(单选组radio)

jquery获取radio值(单选组radio)单选组radio: $("input[@type=radio][@checked]").val();单选组radio: $("input[@type=radio]").attr("checked",'2');//设置value=2的项⽬为当前选中项获取⼀组radio被选中项的值var item = $('input[@name=items][@checked]').val();radio单选组的第⼆个元素为当前选中值$('input[@name=items]').get(1).checked = true单选组 radio: $("input[@type=radio]").attr("checked",'2');//设置value=2的项⽬为当前选中项jquery⽼的版本var_name = $("input[@name='radio_name']:checked").val();jquery 1.3以后的版本var_name = $("input[name='radio_name']:checked").val();看个获取radio值的jquery实例function getra(){var_name = $("input[name='isspecialcnt']:checked").val();//alert(var_name);if(var_name=='1'){$("#isspecialcntyes").show();$("#isspecialcntno").hide();}if(var_name=='0'){$("#isspecialcntyes").hide();() $("#isspecialcntno").show();}}<form name="form1" method="post" action=""><p><label><input type="radio" name="radiogroup1" value="单选" id="radiogroup1_0">单选</label><br><label><input type="radio" name="radiogroup1" value="单选" id="radiogroup1_1">单选</label><br><label><input type="radio" name="radiogroup1" value="单选" id="radiogroup1_2">单选</label><br><label><input type="radio" name="radiogroup1" value="单选" id="radiogroup1_3">单选</label><br><label><input type="radio" name="radiogroup1" value="单选" id="radiogroup1_4">单选</label><br><label><input type="radio" name="radiogroup1" value="单选" id="radiogroup1_5">单选</label><br></p></form>。

jQuery获取Select选择的Text和 Value

jQuery获取Select选择的Text和 Value

一、jQuery获取Select选择的Text和Value:语法解释:1.$("#select_id").change(function(){//code...}); //为Select添加事件,当选择其中一项时触发2.varcheckText=$("#select_id").find("option:selected").text(); //获取Select选择的Text3.varcheckValue=$("#select_id").val(); //获取Select选择的Value4.varcheckIndex=$("#select_id ").get(0).selectedIndex; //获取Select选择的索引值5.varmaxIndex=$("#select_idoption:last").attr("index"); //获取Select最大的索引值二、jQuery设置Select选择的Text和Value:语法解释:1.$("#select_id ").get(0).selectedIndex=1; //设置Select索引值为1的项选中2.$("#select_id ").val(4); // 设置Select的Value值为4的项选中3.$("#select_id option[text='jQuery']").attr("selected", true); //设置Select的Text值为jQuery的项选中三、jQuery添加/删除Select的Option项:语法解释:1.$("#select_id").append("<option value='Value'>Text</option>"); //为Select追加一个Option(下拉项)2.$("#select_id").prepend("<option value='0'>请选择</option>"); //为Select插入一个Option(第一个位置)3.$("#select_idoption:last").remove(); //删除Select中索引值最大Option(最后一个)4.$("#select_id option[index='0']").remove(); //删除Select中索引值为0的Option(第一个)5.$("#select_id option[value='3']").remove(); //删除Select中Value='3'的Option6.$("#select_id option[text='4']").remove(); //删除Select中Text='4'的Option四、获取值1.jquery radio取值,checkbox取值,select取值,radio选中,checkbox选中,select选中,及其相关获取一组radio被选中项的值var item = $('input[@name=items][@checked]').val();2.获取select被选中项的文本:var item = $("select[@name=items] option[@selected]").text();3.select下拉框的第二个元素为当前选中值:$('#select_id')[0].selectedIndex = 1;4.radio单选组的第二个元素为当前选中值:$('input[@name=items]').get(1).checked = true;5.文本框,文本区域:$("#txt").attr("value");6.多选框checkbox:$("#checkbox_id").attr("value");7.单选组radio:$("input[@type=radio][@checked]").val();8.下拉框select:$('#sel').val();五、控制表单元素:1.文本框,文本区域:$("#txt").attr("value",'');//清空内容2.$("#txt").attr("value",'11');//填充内容3.多选框checkbox:$("#chk1").attr("checked",'');//不打勾4.$("#chk2").attr("checked",true);//打勾5.if($("#chk1").attr('checked')==undefined) //判断是否已经打勾6.单选组radio:$("input[@type=radio]").attr("checked",'2');//设置value=2的项目为当前选中项7.下拉框select:$("#sel").attr("value",'-sel3');//设置value=-sel3的项目为当前选中项8.$("<option value='1'>1111</option><option value='2'>2222</option>").appendTo("#sel")//添加下拉框的option9.$("#sel").empty();//清空下拉框10.//遍历option和添加、移除optionfunctionchangeShipMethod(shipping){varlen = $("select[@name=ISHIPTYPE] option").lengthif(shipping.value != "CA"){$("select[@name=ISHIPTYPE] option").each(function(){if($(this).val() == 111){$(this).remove();}});}else{$("<option value='111'>UPS Ground</option>").appendTo($("select[@name=ISHIPTYPE]"));}}//取得下拉選單的選取值$(#testSelectoption:selected').text();或$("#testSelect").find('option:selected').text();或$("#testSelect").val();六、下拉框:1.var cc1 = $(".formc select[@name='country'] option[@selected]").text(); //得到下拉菜单的选中项的文本(注意中间有空格)2.var cc2 = $('.formc select[@name="country"]').val(); //得到下拉菜单的选中项的值3.var cc3 = $('.formc select[@name="country"]').attr("id"); //得到下拉菜单的选中项的ID属性值4.$("#select").empty();//清空下拉框//$("#select").html('');5.$("<option value='1'>1111</option>").appendTo("#select")//添加下拉框的option稍微解释:1.select[@name='country'] option[@selected] 表示具有name 属性,并且该属性值为'country' 的select元素里面的具有selected 属性的option 元素;可以看出有@开头的就表示后面跟的是属性。

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

/code/snippet_179497_6635
$("input[name='items']:checked").val();
004另:判断radio是否选中并取得选中的值
005
006如下所示:
007function checkradio(){
008var item = $(":radio:checked");
009var len=item.length;
010if(len>0){
011 alert("yes--选中的值为:"+$(":radio:checked").val()); 012}
013}
014
015
016
017
018 jquery radio取值,checkbox取值,select取值,radio选中,checkbox 选中,select选中,及其相关
019
020获取一组radio被选中项的值
021
022 var item = $('input[name=items][checked]').val();
023
024获取select被选中项的文本
025
026 var item = $("select[name=items] option[selected]").text(); 027
028 select下拉框的第二个元素为当前选中值
029
030 $('#select_id')[0].selectedIndex = 1;
031
032 radio单选组的第二个元素为当前选中值
033
034 $('input[name=items]').get(1).checked = true;
035
036
037获取值:
038
039
040
041文本框,文本区域:$("#txt").attr("value");
042
043多选框checkbox:$("#checkbox_id").attr("value");
044
045单选组radio: $("input[type=radio][checked]").val(); 046
047下拉框select: $('#sel').val();
048
049
050
051控制表单元素:
052
053文本框,文本区域:$("#txt").attr("value",'');//清空内容
054
055$("#txt").attr("value",'11');//填充内容
056
057
058多选框checkbox: $("#chk1").attr("checked",'');//不打勾
059
060 $("#chk2").attr("checked",true);//打勾
061
062 if($("#chk1").attr('checked')==undefined) //判断是否已经打勾063
064
065
066
单选组radio: $("input[type=radio]").attr("checked",'2');//设置value=2的项目为当前选中项
067
068
下拉框select: $("#sel").attr("value",'-sel3');//设置value=-sel3的项目为当前选中项
069
070 $("<option value='1'>1111</option><option
value='2'>2222</option>").appendTo("#sel")//添加下拉框的option
071
072$("#sel").empty();//清空下拉框073
074
075
076刚开始接触jquery,很多东西不熟悉077
078
在用$("#id")来获得页面的input元素的时候,发现$("#id").value不能取到值
079
080
081
082后来终于在伟大的百度帮助下,找到了问题的原因:083
084 $("")是一个jquery对象,而不是一个dom element 085
086
087
088 value是dom element的属性
089
090
091
092 jquery与之对应的是val
093
094
095 val() :获得第一个匹配元素的当前值。

096
097
098
099 val(val):设置每一个匹配元素的值。

100
101
102
103所以,代码应该这样写:
104
105
106取值:val = $("#id")[0].value;
107赋值: $("#id")[0].value = "new value";
108
109或者$("#id").val("new value");
110
111
112
113或者这样也可以:val = $("#id").attr("value");
114
115
116
117jQuery中each非常好用,常用它取代javascript的for循环118
119例如在一个function里有一个each,在each里某种条件成立的话,就把这个function返回true或者false
120
121function methodone(){ 122....
123$.each(array,function(){ 124if(条件成立){
125return true;
126}
127});
128....
129}
130
131结果发现老是不对。

132
133后来查找资料才发现,在each代码块内不能使用break和continue,要实现break和continue的功能的话,要使用其它的方式
134break----用return false; 135continue--用return ture; 136
137所以当我在each里想使用return true给这个function返回时,其实只是让each继续执行而以
138连each都没有中断,所以function也就不能return了
139
140另:判断radio是否选中并取得选中的值
141
142如下所示:
143function checkradio(){
144var item = $(":radio:checked");
145var len=item.length;
146if(len>0){
147 alert("yes--选中的值为:"+$(":radio:checked").val()); 148}
149}。

相关文档
最新文档