工程實績多,嚴選防水建材,加強房屋外牆防水功能
免費到府估價,售後長期服務。
平價壁紙多年豐富專業,拆除各類房型技巧純熟,快速有效率
免費現場估價,值得您信賴

首頁  •  j2h 論壇 • 程式設計討論     • 

[JQuery] 取得 select 的 value 跟 text

房東:阿凱
發表時間:2011-03-26
[檢舉]


1. 首先如何取得 select 的 value 跟 text



/*

*

* 取得 select value 值

*/


$(\'#selectList\').val();


取得 text 值,可以利用 :selected 這個



/*

*

* 取得 select text 值

*/


$(\'#selectList :selected\').text();




底下是一個範例,實做選取 select options 加入到另一個 select:

html 部份:



<select id="test1" name="test1" />

  <option value="1">中正大學</option>

  <option value="2">台灣大學</option>

  <option value="3">交通大學</option>

</select>

<select id="test2" />

  <option value="0">請選擇</option>

</select>


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





  • 贊助網站       

    廣利不動產-板橋在地生根最實在--新板特區指名度最高、值得您信賴的好房仲
    完整房訊,房屋、店面熱門精選物件,廣利不動產 優質仲介,房屋租賃、買賣資訊透明,交易真安心!
    廣利不動產-新板特區指名度最高、值得您信賴的好房仲
    您的托付,廣利用心為您服務



  •  共 0 人回應

    姓名:
    佈告內容: