JavaScript jquery A common walkthrough of form elements _javascript tips

Source: Internet
Author: User

1. dropdown box select:

Remove option

$ ("#ID option"). each (the function () {
 if ($ (this). val () = =) {
 $ (this). Remove ();
 }
});

Add option

$ ("<option value= ' >ups ground</option>"). Appendto ($ ("#ID"));

Get the selected value of the dropdown menu

Remove the text
$ (' #testSelect option:selected ') from the Pull. Text ();
$ ("#testSelect"). Find (' option:selected '). Text ();
Document.all.objselect.options[document.all.objselect.selectedindex].text; JS Operation objselect for select name//to obtain the Drop-down value
$ ("#testSelect"). Val ();
JS Operation
document.getElementById (' Objselect '). value=2;
Document.all.objSelect.value;

Select the Drop-down box based on the value of option

$ (' #testSelect '). Val (' a ');d ocument.all.objSelect.value = Objitemvalue; JS Operation objselect for select name or Iddocument.getelementbyid (' sel '). Value=objitemvalue;

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

$ (' #select_id ') [0].selectedindex = 1;

2, Radio Box Radio :

$ ("input[type=radio][checked]"). Val (); Gets the value of the selected item in the Radio box (note that there are no spaces in the middle)
$ (': radio[name= ' Radio ']:checked '). Val (); the second method

$ ("input[type=radio][value=2]" ). attr ("Checked", ' checked '); Sets the value=2 of the radio box to the selected state. (Note that there are no spaces in between)
$ (': radio[value= "2"]). attr (' checked ', ' checked ');

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

$ ("Input[name= ' items ']"). Get (1). checked = true;

3, check box checkbox : (Other wording see above: Radio)

$ ("input[type= ' checkbox '][checked]"). Val (); Gets the value of the first item selected in the check box
$ ("#chk1"). attr ("Checked", "");//Do not tick
$ ("#chk2"). attr ("Checked", true);
//uncheck/Select All

 $ ("#selectAll"). Bind (' click ', Function () {
   var sa = $ (this). attr ("checked");
   $ ("Input[name= ' sel[]"). each (function () {
     this.checked=sa;
   });
 

4, Input box inputs:

$ (': input[name= "keyword"]). Val ();//Get value based on name value

5 , text box textarea :

Fixed text box size:
<textarea name= "123" style= "Resize:none;" ></textarea>

Above this javascript jquery to form element Common operation detailed explanation is small series to share to everybody's content, hoped can give everybody a reference, also hoped that everybody supports the cloud habitat community.

Related Article

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.