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

Source: Internet
Author: User

< no details >

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 6667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611 7118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149 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或者falsefunction methodone(){ .... $.each(array,function(){ if(条件成立){ return true; } }); .... } 结果发现老是不对。 后来查找资料才发现,在each代码块内不能使用breakcontinue,要实现breakcontinue的功能的话,要使用其它的方式 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.