JQuery Radio value, checkbox value, select Value

Source: Internet
Author: User

Syntax Explanation:

1 $("#select_id").change(function(){//code...});   //为Select添加事件,当选择其中一项时触发
2 varcheckText=$("#select_id").find("option:selected").text();  //获取Select选择的Text
3 varcheckValue=$("#select_id").val();  //获取Select选择的Value
4 varcheckIndex=$("#select_id ").get(0).selectedIndex;  //获取Select选择的索引值
5 varmaxIndex=$("#select_id option:last").attr("index");  //获取Select最大的索引值

jquery sets the text and value selected for select:

Syntax Explanation:

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 Add/Remove option for select:

Syntax Explanation:

1 $("#select_id").append("<option value=‘Value‘>Text</option>");  //为Select追加一个Option(下拉项)
2 $("#select_id").prepend("<option value=‘0‘>请选择</option>");  //为Select插入一个Option(第一个位置)
3 $("#select_id option:last").remove();  //删除Select中索引值最大Option(最后一个)
4 $("#select_id option[index=‘0‘]").remove();  //删除Select中索引值为0的Option(第一个)
5 $("#select_id option[value=‘3‘]").remove();  //删除Select中Value=‘3‘的Option
6 $("#select_id option[text=‘4‘]").remove();  //删除Select中Text=‘4‘的Option

jquery Radio value, checkbox value, select Value, Radio selected, CheckBox selected, select selected, and its related to get a set of radio values of the selected item

1 varitem = $(‘input[@name=items][@checked]‘).val();

Gets the text of the Select selected item

1 varitem = $("select[@name=items] option[@selected]").text();

The second element of the Select drop-down box is the currently selected value

1 $(‘#select_id‘)[0].selectedIndex = 1;

Radio the second element of a radio group is the currently selected value

1 $(‘input[@name=items]‘).get(1).checked = true;

Get Value:

    • text box, text area: $ ("#txt"). attr ("value");
    • Multi Box checkbox:$ ("#checkbox_id"). attr ("value");
    • Radio Group Radio: $ ("input[@type =radio][@checked]"). Val ();
    • Drop-down box select: $ (' #sel '). Val ();

Control form elements:

text box, text area:

$ ("#txt"). attr ("Value", "");//Clear Contents

$ ("#txt"). attr ("value", ' 11 ');//fill content

Multiple marquee checkbox: $ ("#chk1"). attr ("Checked", "");//No Tick

$ ("#chk2"). attr ("Checked", true);//tick

if ($ ("#chk1"). attr (' checked ') ==undefined)//Determine if a tick has been made

Radio Group Radio: $ ("input[@type =radio]"). attr ("Checked", ' 2 ');//Set value=2 for the currently selected item

Drop-down box select: $ ("#sel"). attr ("value", '-sel3 ');//Set the VALUE=-SEL3 item as the current selection

$ ("<option value= ' 1 ' >1111</option><option value= ' 2 ' >2222</option>"). AppendTo ("#sel")// option to add a drop-down box

$ ("#sel"). empty ();//Empty drop-down box

Traverse option and Add, remove option

01 functionchangeShipMethod(shipping) {
02     varlen = $("select[@name=ISHIPTYPE] option").length
03     if(shipping.value != "CA") {
04         $("select[@name=ISHIPTYPE] option").each(function() {
05             if($(this).val() == 111) {
06                 $(this).remove();
07             }
08         });
09     else{
10         $("<option value=‘111‘>UPS Ground</option>").appendTo($("select[@name=ISHIPTYPE]"));
11     }
12 }

Gets the selected value of the drop-down box

$ (#testSelect option:selected '). Text ();

or $ ("#testSelect"). Find (' option:selected '). Text ();

or $ ("#testSelect"). Val ();

check box, we recommend the following Operation method

$ ("#chk2") [0].checked = true;

$ ("#chk2") [0].checked = false;

Transferred from: http://www.cnblogs.com/shiyou00/p/5577329.html

JQuery Radio value, checkbox value, select Value

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.