php 获取下拉框选中的文本,jQuery实现select下拉框获取当前选中的文本
本文主要介紹了jQuery實現select下拉框獲取當前選中文本、值、索引以及添加/刪除Select的Option項的相關知識,具有很好的參考價值。下面跟著小編一起來看下吧,希望能幫助到大家。
話不多說,請看代碼://直接保存后綴.htnl用谷歌瀏覽器打開,親測有效
$(function(){
//為Select添加事件,當選擇其中一項時觸發
$("select:eq(0)").change(function(){
//code
});
//獲取Select選中的Text:結果是由所有匹配元素包含的文本內容組合起來的文本
var checkText = $("select:eq(0) :selected").text();//建議用這個簡單
= $("select:eq(0) option:selected").tetx();
= $("#One").find(":selected").text();
= $("#One").find("option:selected").text();
//如果多選,將返回一個數組,其包含所選的值。
var checkValue = $("#select_id").val();
//獲取Select選中匹配元素的當前值,即[即使多選也只]取得第一個匹配元素的val內容
var checkValue = $("select:eq(0) :selected").val();//=========強烈建議用這個,以防多選
//獲取Select選中的索引值
var checkIndex = $("#select_id ").get(0).selectedIndex;
//獲取Select最大的索引值
var maxIndex = $("#select_id :last").prop("index"); //建議用這個
= $("#select_id option:last").prop("index");
= $("select:eq(0)").find(":last").prop("index")
= $("select:eq(0)").find("option:last").prop("index")
//=========================================================================================
//jQuery設置Select選擇的 Text和Value:
// 設置Select的Value值為4的項選中
$("#select_id ").val(4); //用這個
$("#select_id [value='4']").prop("selected", true);
$("#select_id option[value='4']").prop("selected", true);
//設置select中的第一個option被選中
$("select :first").prop("selected", true);//這個
$("select :first").prop("selected", 'selected');
$("select option:first").prop("selected", "true");
$("select option:first").prop("selected", "selected");
//============================================================================================
//jQuery添加/刪除Select的Option項
$("#select_id").append("Text"); //為Select末尾追加一個Option(下拉項)
$("#select_id").prepend("請選擇"); //為Select首部插入一個Option(第一個位置)
$("#select_id :last").remove(); //刪除Select中索引值最大Option(最后一個)
$("#select_id :fist").remove(); //刪除Select中索引值最小為0Option(第一個)
$("#select_id [value='3']").remove(); //刪除Select中Value='3'的Option
});
蘋果 香蕉 草莓 橘子 | 葡萄 |
相關推薦:
總結
以上是生活随笔為你收集整理的php 获取下拉框选中的文本,jQuery实现select下拉框获取当前选中的文本的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 披马甲送福利~~免费帮各位TYER们看淘
- 下一篇: 同是相差14岁,为什么周杰伦昆凌被祝福,