jquery Get Set Empty Select selected text and value _jquery

Source: Internet
Author: User

Get select:
Gets the text selected by select:

$ ("#ddlregtype"). Find ("option:selected"). Text ();

Gets the value selected by select:

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

Gets the index selected by select:

$ ("#ddlregtype"). Get (0). SelectedIndex;

Set Select:
Set the index selected by select:

$ ("#ddlregtype"). Get (0). Selectedindex=index;//index is the index value

Set the value selected by select:

$ ("#ddlregtype"). attr ("value", "normal");
$ ("#ddlregtype"). Val ("normal");
$ ("#ddlregtype"). Get (0). value = value;

Set selected text for select:

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:

$ ("#select_id"). Append ("<option value= ' value ' >text</option>"); Add an option
$ ("#select_id"). Prepend ("<option value= ' 0 ' > Please select </option>"); Insert an option
$ ( "#select_id option:last"). Remove (); Deletes the maximum index value of option
$ ("#select_id option[index= ' 0 ']"). Remove ()//delete option
$ ("#select_id option[with index value 0" Value= ' 3] "). Remove (); Delete option
$ ("#select_id option[text= ' 4 ']" with a value of 3. Remove ();//delete option with text value 4

Empty select:

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

Work required, to get the values from two forms. As shown in figure:

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 ();
$ (function () {
$ (' #select1 '). each (///Get Select1 of all value
function () {
$ (' button '). Click (function () {
Alert ($ (' #select2 '). Val ()); Get the Select1 value in Select2
});
}
</script>

It's worth noting that you can't write directly

$ (function () {
$ (' #select2 '). each (///Get all the values for Select1 because the preceding option is added from the left to the right, 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:

<div class= "Centent" >
<select multiple= "multiple" id= "Select1" name= "dd" style= "width:100px;height : 160px; " >
<option value= "1" > Options 1</option>
<option value= "2" > Options 2</option>
< Option value= "3" > Options 3</option>
<option value= "4" > Options 4</option> <option
"5" > Options 5</option>
<option value= "6" > Options 6</option>
<option value= "7" > Options 7</option >
</select>
<div>
<span id= "Add" > Selected add to right >></span>
<span Id= "Add_all" > All add to Right >></span>
</div>
</div>
<div class= "Centent" >
<select multiple= "multiple" id= "Select2" name= "sel" style= "width:100px;height:160px"; >
</select>
<div>
<span id= "Remove" ><< check Delete to left </span>
< Span id= "Remove_all" ><< all deleted to left </span>
</div>
</div>

Use Jquery,ajax to invoke the option options for dynamically populating a Select

/binding ClassLevel1 Click event $ ("#ClassLevel1"). Change (function () {var id = $ ("#ClassLevel1"). val
();
var Level2 = $ ("#ClassLevel2");
Level2.empty ();
$ ("#ClassLevel3"). Hide (); $.ajax ({url:./askcommon.ashx?action=getclasslevel&pid= + ID, data: {"type": "Ajax"}, DataType: "JSON", type: "GE T ", success:function (data) {var json = eval_r (data); for (Var ind in JSON) {Level2.append ($ (" <option value= ' "+ JSO
N[ind].id + "' >" + json[ind].typename + "</option>");
}

}
}); })

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.