select 控制項的操作是表單開發中的常見需求,以下整理幾個常用的取值與設值技巧。
1. 級聯 select 的操作,讓後一個 select 的選項隨著前一個選中值的變化而改變。
$(".select_A").change(function(){ $(".select_B").empty();// 清空後一個select var option = $("<option>").val(1).text('xxx');// 按照實際業務要求重新賦值option $(".select_B").append(option); });
2. 獲取 select 選中項的 value 值:
$(".select").val();
3. 獲取 select 選中項的 text 文字:
$(".select").find("option:selected").text();
4. 設定 select 選中對應的 value 值項、或對應的 text 文字項:
$(".select").val('xxx'); $(".select").find("option[text='xxx']").prop('selected',true);
以上就是本次分享的全部內容,希望對你的學習或工作有所幫助。