想讓下拉選單的選擇即時反映到旁邊的文字標籤上,只要監聽 change 事件即可。
$(function() { $("#DropDownList1").bind("change", function() { $("#Label1").text($("#DropDownList1 option:selected").text()); }); });
$("#DropDownList1").bind("change", function() { $("#Label1").text($("#DropDownList1").val()); });
兩者的差別在於取的是文字還是值,可視需求挑選。補充說明取值方式:
var name = $("#DropDownList1 option:selected").val(); // 取得選中項的值 var name1 = $("#DropDownList1").text(); // 取得下拉選單的全部文字
只要掌握 option:selected 這個選擇器,就能輕鬆讀取使用者當前的選擇。