<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