Combination of Ajax and select labels

Source: Internet
Author: User
<Form> <select name = "ID"> <option value = "0"> none </option> <option value = "1"> Option 1 </option> <Option value = "2"> default value: 2 </option> <option value = "3"> default value: 3 </option> </SELECT> <button> submit </button> </ form>

 

/*
| ---------------------------------------------------------------------------------
| Idea 1: use the change event to set the value of the first option and obtain the value of the first option at the time of submission.
| @ Blackeye poet <www. chenwei. ws>
| ---------------------------------------------------------------------------------
*/
$ ('Select '). change (function () {var Options = $ ("select "). children (); // All option objects othis = $ (this); // The current option object $ ('select '). children ('option: eq (0 )'). val (othis. val (); // set the value of the first option

VaR id = othis. Val (); // value of the first option
VaR text = othis. Text (); // select option text
$ ('Button '). Click (function (){
$. Post ('www. chenwei. ws', {ID: ID, text: text}, function (data ){
//...........
});
});
})

Problems: 1. When the Chang event is used and the default option is set to 'None' again, the value of the first option is no longer 0.
/*
| ---------------------------------------------------------
| Train of thought 2: Use the selected Attribute of option to mark the selection by adding and removing this attribute
| @ Blackeye poet <www. chenwei. ws>
| ---------------------------------------------------------
*/
$ ('Select '). children (). click (function (data) {var Options = $ ('select'), children (); othis = $ (this); options. removeattr ('selected'); othis. ATTR ({selected: 'true'}); var id = othis. val (); var text = $ ("option [Selected = 'true']"). text (); $ ('button '). click (function () {$. post ('www. chenwei. WS ', {ID: ID, text: text}, function (data ){//............});});}) problems: 1. the original select mechanism is changed, and the selected value cannot be displayed.
/*
| Bytes ---------------------------------------------------------------------------------------
| Train of Thought 3: without changing the operation, you can directly obtain the id value of the select tag. The text is the default option text. When you click it, you can obtain the new option text.
| @ Blackeye poet <www. chenwei. ws> | author ---------------------------------------------------------------------------------------
*/
VaR select = $ ('select'); var option = select. Children ('option: eq (0) '); var detail = option. Text (); // initial text
Select. children (). click (function () {detail = $ (this ). text (); // obtain the text dynamically if any modification is made.}); $ ('click '). click (function () {var id = select. val (); // directly obtain the select ID, that is, the submitted ID var detail = detail; $. post ('www. chenwei. WS ', {ID: ID, detail: Detail}, function (data ){//.............})});

No problems were found in the combination of Ajax commit and select tag implemented by 'train of thought 3.

Bytes --------------------------------------------------------------------------------------------------------

Category: HTML/CSS/JS/jquery tags: HTML/CSS/JS/jquery ..

Combination of Ajax and select labels

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.