1. 首先如何取得 select 的 value 跟 text
/*
*
* 取得 select value 值
*/
$('#selectList').val();
取得 text 值,可以利用 :selected 這個
/*
*
* 取得 select text 值
*/
$('#selectList :selected').text();
底下是一個範例,實做選取 select options 加入到另一個 select:
html 部份:
jQuery 部份:
$("#test1").change(function(){
/*
* $(this).val() : #test1 的 value 值
* $('#test1 :selected').text() : #test1 的 text 值
*/
$("#test2").addOption($(this).val(), $('#test1 :selected').text());
});
參考文章:
jQuery Tip – Getting Select List Values
|