jquery Gets the dropdown box value and select text

Source: Internet
Author: User

Gets the text selected by select:

The code is as follows Copy Code
$ ("#ddlregtype"). Find ("option:selected"). Text ();

Gets the value selected by select:

The code is as follows Copy Code
$ ("#ddlregtype"). Val ();

Gets the index selected by select:

The code is as follows Copy Code
$ ("#ddlregtype"). Get (0). SelectedIndex;

Set Select:

Set the index selected by select:

The code is as follows Copy Code
$ ("#ddlregtype"). Get (0). Selectedindex=index;//index is the index value

Set the value selected by select:

The code is as follows Copy Code

$ ("#ddlregtype"). attr ("value", "normal");

$ ("#ddlregtype"). Val ("normal");

$ ("#ddlregtype"). Get (0). value = value;

Set selected text for select:

The
  code is as follows copy code

var count=$ ("#ddlregtype option"). Length;

  for (var i=0;i<count;i++)  
     {            if ($ ("#ddlregtype"). Get (0). Options[i].text = text)  
         { 
            $ ("#ddlregtype" ). Get (0). options[i].selected = true; 
         
             break; 
        } 
   }

 

$ ("#select_id option[text= ' jquery ']"). attr ("selected", true);

To set the SELECT option item:

The code is as follows Copy Code

$ ("#select_id"). Append ("<option value= ' value ' >text</option>"); Add an option

$ ("#select_id"). Prepend ("<option value= ' 0 ' > Please select </option>"); Insert an option before

$ ("#select_id option:last"). Remove (); Delete the maximum index value option

$ ("#select_id option[index= ']"). Remove ()//delete option with index value of 0

$ ("#select_id option[value= ' 3 ']"). Remove (); Delete option with a value of 3

$ ("#select_id option[text= ' 4 ']"). Remove (); Delete option with text value of 4

Empty select:

$ ("#ddlregtype"). empty ();



How do I get the value from the left selection box to the right selection box? I think I want to use the web effects can get, here with the more popular jquery.


The JS code is as follows:
// Get all property values var item = $ ("#select1"). Val ();
The code is as follows Copy Code
$ (function () {
$ (' #select1 '). each (//get all the values of the Select1
function () {
$ (' button '). Click (function () {
Alert ($ (' #select2 '). Val ()); Get the Select1 value in Select2
});
});

})

</script>
It's worth noting that you can't write directly
The code is as follows Copy Code
$ (function () {
$(' #select2 '). each (//) gets all the values for Select1 because the preceding option is added to the right from the left, and jquery does not actually pass the value from the left to the right.
function () {
$ (' button '). Click (function () {
Alert ($ (this). Val ()); Get the Select1 value in Select2
});
});

})
Html:
The code is as follows Copy Code
<div class= "Centent" >
<select multiple= "multiple" id= "Select1" name= "dd" style= "width:100px;height:160px"; >
<option value= "1" > Option 1</option>
<option value= "2" > Option 2</option>
<option value= "3" > Option 3</option>
<option value= "4" > Option 4</option>
<option value= "5" > Option 5</option>
<option value= "6" > Option 6</option>
<option value= "7" > Option 7</option>
</select>
<div>
<span id= "Add" > select Add to Right &gt;&gt;</span>
<span id= "Add_all" > All add to Right &gt;&gt;</span>
</div>
</div>

<div class= "Centent" >
<select multiple= "multiple" id= "Select2" name= "sel" style= "width:100px;height:160px"; >

</select>
<div>
<span id= "Remove" >&lt;&lt; select Remove to left </span>
<span id= "Remove_all" >&lt;&lt; all deleted to left </span>
</div>
</div>

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.