- get
- ¨ú¥X¿ï¾ÜªºÈ
1
2
$(
"select#Club"
).val();
$(
'select#Club option:selected'
).text();
¥H¤W2¤èªk¦b³æ¿ï®É¬Û¦P¡A¦ý½Æ¿ï®É¡A
val()·|¥Î³r¸¹¤À¶} ex. AA, BB
text()¤£·| ex. AABB - ¨ú¥Xarray
1
2
3
$(
"select#Club"
).children(
"[@selected]"
).each(
function
(){
alert(
this
.text());
});
- ¨ú¥X¿ï¾ÜªºÈ
- set
- ¨Ï¬YoptionÅܬ°selected
1
2
3
4
5
6
7
8
9
$(
"#select1"
).children().each(
function
(){
if
($(
this
).text()==
"option you want"
){
//jQueryµ¹ªk
$(
this
).attr(
"selected"
,
"true"
);
//©Î¬Oµ¹selected¤]¥i
//javascriptµ¹ªk
this
.selected =
true
;
}
});
- Åý·s¼Wªºoptionª½±µ¬°selected
1
2
3
4
var
option = jQuery(
"new option"
);
$(
'select#Club'
).append(option);
$(option).attr(
"selected"
,
"true"
);
//Åýoption¬°selected
$(
'select#Club'
).trigger(
"change"
);
//³Ì«ánIJµoselectªºchange¨Æ¥ó
- select¤U©Ô®Øªº²Ä¤GÓ¤¸¯À¬°·í«e¿ï¤¤È
$('select#Club)[0].selectedIndex = 1;//¤£ª¾¬°¦ón¥[[0]
=========== K. T. Chen ´£¨ì ==========================
¦b$("")¥[[0]ªº·N«ä¬O§âjQueryª«¥óÂରDOMª«¥ó¡C³o¼Ë¤ljQueryª«¥ó¤~¯à¨Ï¥ÎDOM©³¤UªºselectedIndex¤èªk¡C
- ¨Ï¬YoptionÅܬ°selected
- event
//§ïÅܮɪº¨Æ¥ó
1
2
3
4
5
6
7
8
9
$(
"select#Club"
).change(
function
(){
//¨Æ¥óµo¥Í
//¤@¦¸¦L¥X
alert($(
this
).val());
//¦L¥X¿ï¨ì¦hÓÈ
jQuery(
'option:selected'
,
this
).each(
function
(){
alert(
this
.value);
});
});
- ²¾°£ removeOption(index/value/regex[, selectedOnly])
1
$(
'select#Clubs option:selected'
).remove();
//¯Âjavascirpt
1 2 3 4 | <select onchange= "alert('Index: ' + this.selectedIndex + '\nValue: ' + this.options[this.selectedIndex].value)" > ... </select> |