Combined Use of Ajax and select tags, ajaxselect tags

Source: Internet
Author: User

Combined Use of Ajax and select tags, ajaxselect tags

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

The following table lists the select form and submits the form data using Ajax:

<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 --------------------------------------------------------------------------------------------------------


I used Ajax to return a select tag. How can I obtain the value of this tag?

It is the same as the normal method.
Assume that the returned select id is "mySelect ",
The obtained value is
$ ("# MySelect"). val ();

Select tag + ajax implementation Association in struts2

Tag an ONCHANGE event, get the current value in the event, use this value to retrieve it from the background, and then display it in input.
 

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.