jquery is actually applied to determine if Radio,selelct,checkbox is selected and selected values

Source: Internet
Author: User

jquery取radio单选按钮的值  $( "input[name=‘items‘]:checked" ).val();  另:判断radio是否选中并取得选中的值 如下所示: function checkradio(){ var item = $( ":radio:checked" ); var len=item.length; if (len>0){    alert( "yes--选中的值为:" +$( ":radio:checked" ).val()); } }   jquery 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的项目为当前选中项     $( "<option value=‘1‘>1111</option><option value=‘2‘>2222</option>" ).appendTo( "#sel" ) //添加下拉框的option   $( "#sel" ).empty(); //清空下拉框         刚开始接触jquery,很多东西不熟悉     在用$( "#id" )来获得页面的input元素的时候,发现$( "#id" ).value不能取到值        后来终于在伟大的百度帮助下,找到了问题的原因:     $( "" )是一个jquery对象,而不是一个dom element         value是dom element的属性       jquery与之对应的是val        val() :获得第一个匹配元素的当前值。       val(val):设置每一个匹配元素的值。        所以,代码应该这样写:        取值:val = $( "#id" )[0].value;     赋值:  $( "#id" )[0].value = "new value" ;     或者$( "#id" ).val( "new value" );        或者这样也可以:val = $( "#id" ).attr( "value" ); jQuery中each非常好用,常用它取代javascript的 for 循环 例如在一个 function 里有一个each,在each里某种条件 成立的话,就把这个 function 返回 true 或者 false function methodone(){ .... $.each(array, function (){ if (条件成立){ return true ; } }); .... } 结果发现老是不对。 后来查找资料才发现,在each代码块内不能使用 break continue ,要实现 break continue 的功能的话,要使用其它的方式 break ----用 return false ; continue --用 return ture; 所以当我在each里想使用 return true 给这个 function 返回时,其实只是让each继续执行而以 连each都没有中断,所以 function 也就不能 return 了  另:判断radio是否选中并取得选中的值 如下所示: function checkradio(){ var item = $( ":radio:checked" ); var len=item.length; if (len>0){    alert( "yes--选中的值为:" +$( ":radio:checked" ).val()); } }

jquery is actually applied to determine if Radio,selelct,checkbox is selected and selected values

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.