[jQuery] 如何取得 select List index 和 value 值
Mar 20th, 2009 by appleboy 參觀者:19,173Views 機器人:324Views
推到 Twitter!
推到 Plurk!
推到 Facebook!
上次寫了 [jQuery] 表單取值 radio checkbox select text 驗證表單,這篇淺顯易懂,在 ptt 有人問到如何把 select 的 value 跟 text 值加入到另一個 select 的 options 裡面,其實還蠻簡單的,利用 jQuery – Select box manipulation 這個 plugin 就可以輕鬆做到了,底下是我的一些筆記心得:
1. 首先如何取得 select 的 value 跟 text
/*
*
* 取得 select value 值
*/
$('#selectList').val();
*
* 取得 select value 值
*/
$('#selectList').val();
取得 text 值,可以利用 :selected 這個
/*
*
* 取得 select text 值
*/
$('#selectList :selected').text();
*
* 取得 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());
});
/*
* $(this).val() : #test1 的 value 值
* $('#test1 :selected').text() : #test1 的 text 值
*/
$("#test2").addOption($(this).val(), $('#test1 :selected').text());
});
參考文章:
jQuery Tip – Getting Select List Values

您好~
我看您的文章,覺得你對 JQ 研究蠻多的
我最近遇到一個問題
想問您看看您是否知道
就是我遇到一個網頁 A網站
我需要從我的網頁去取一些資料回來 B網站
但是A網站有驗證 REFERRER 標頭
所以我想問 JQ 是否可以修改 HEADER 的 REFERRER 這個屬性
因為我看 1.2 版本還有可以設定
不過我自己測試以後發現 REFERRER 這個屬性就算更改了也沒反應
不知道是我寫錯 還是安全性不允許了?
= =~~
不知道可否向您請教一下
–
corAusir 程式逗設計
提供平面設計 與 程式設計 的資料~
blog.corausir.org
或點我的名字來我家 ^^
標題打錯字了,我想應該是 select “List” index 和 value
我修正標題,感謝你 Chui-Wen Chiu
您好
想請教您 以下這段程式 可不可以直接用response當參數,新增一項option嗎
因為執行網頁 左下資訊欄都顯示網頁錯誤 且此select也沒動靜
下面TableShowID是select 的id
success: function(response)
{
$(’#TableShowID’).addOption(response,response);
}