JQuery operates Select options to move up and down, remove and add, etc.

Source: Internet
Author: User

Copy codeThe Code is as follows:
<Meta http-equiv = "content-type" content = "text/html; charset = UTF-8">

<Script type = "text/javascript" src = "jquery-1.9.1.min.js"> </script>

<Script type = "text/javascript">
/**
* Move the selected option up
*/
Function upSelectedOption (){
If (null ==$ ('# where'). val ()){
Alert ('select an Project ');
Return false;
}
// Selected index, starting from 0
Var optionIndex = $ ('# where'). get (0). selectedIndex;
// If the selected part is not at the top of the list, it indicates that it can be moved.
If (optionIndex> 0 ){
$ ('# Where option: selected'). insertBefore ($ (' # where option: selected'). prev ('option '));
}
}

/**
* Move the selected option downward.
*/
Function downSelectedOption (){
If (null ==$ ('# where'). val ()){
Alert ('select an Project ');
Return false;
}
// Index length, starting from 1
Var optionLength = $ ('# where') [0]. options. length;
// Selected index, starting from 0
Var optionIndex = $ ('# where'). get (0). selectedIndex;
// If the selected part is not at the bottom, it indicates that it can be down.
If (optionIndex <(optionLength-1 )){
$ ('# Where option: selected'). insertAfter ($ (' # where option: selected'). next ('option '));
}
}

/**
* Remove the selected option.
*/
Function removeSelectedOption (){
If (null ==$ ('# where'). val ()){
Alert ('select an Project ');
Return false;
}
$ ('# Where option: selected'). remove ();
}

/**
* Obtain all option values
*/
Function getSelectedOption (){
// Obtain the selected Text
Var checkText = $ ('# where'). find ('option: selected'). text ();
// Obtain the Value selected by the Select statement.
Var checkValue = $ ('# where'). val ();
Alert ('selected text = '+ checkText +', value = '+ checkValue );
Var ids = '';
Var options = $ ('# where') [0]. options;
For (var I = 0; I <options. length; I ++ ){
Ids = ids + ''' + options [I]. id;
}
Alert ('the order of the currently selected numbers is '+ ids );
}

/**
* Add option
*/
Function addSelectedOption (){
// Add to the first position
$ ('# Where'). prepend (' <option value = "hbin" id = "where06"> Haerbin </option> ');
// Add to the last position
$ ('# Where'). append (' <option value = "hlj" id = "where07"> HeiLongJiang </option> ');
$ ('# Where'). attr ('SIZE', 7 );
}
</Script>

<Div id = "updown">
<Select id = "where" name = "where" size = "5">
<Option value = "hk" id = "where01"> Hong Kong </option>
<Option value = "tw" id = "where02"> Taiwan </option>
<Option value = "cn" id = "where03"> China </option>
<Option value = "us" id = "where04"> United States </option>
<Option value = "ca" id = "where05"> Canada </option>
</Select>
</Div>
<Br/>
<Input type = "button" value = "Move Up" onclick = "upSelectedOption ()"/>
<Input type = "button" value = "Move Down" onclick = "downSelectedOption ()"/>
<Input type = "button" value = "delete" onclick = "removeSelectedOption ()"/>
<Input type = "button" value = "OK" onclick = "getSelectedOption ()"/>
<Input type = "button" value = "add" onclick = "addSelectedOption ()"/>

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.