溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

如何獲取select下拉框的選中值

發(fā)布時(shí)間:2021-07-10 14:16:18 來源:億速云 閱讀:228 作者:chen 欄目:編程語言

這篇文章主要講解了“如何獲取select下拉框的選中值”,文中的講解內(nèi)容簡(jiǎn)單清晰,易于學(xué)習(xí)與理解,下面請(qǐng)大家跟著小編的思路慢慢深入,一起來研究和學(xué)習(xí)“如何獲取select下拉框的選中值”吧!

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<script src="http://libs.baidu.com/jquery/1.7.2/jquery.min.js"></script>
<!--<script src="http://libs.baidu.com/jquery/1.6.4/jquery.min.js"></script>-->
<!--<script src="http://libs.baidu.com/jquery/1.5.2/jquery.min.js"></script>-->
<!--<script src="http://libs.baidu.com/jquery/1.4.4/jquery.min.js"></script>-->
<!--<script src="http://libs.baidu.com/jquery/1.3.2/jquery.min.js"></script>-->
<!--<script src="http://libs.baidu.com/jquery/1.2.3/jquery.min.js"></script>-->
<style type="text/css">
</style>
</head>
<body>
<form action="" id="form">
<select id="sl">
<option selected="true" value="0">第一個(gè)</option>
<option value="1">第二個(gè)</option>
<option value="2">第三個(gè)</option>
</select>
</form>
</body>
</html>
<script type="text/javascript" language="javascript">
//獲取值
alert($("#sl option:selected").val());
alert($("#sl").find("option:selected").text());
alert($("#sl").val());

//獲取選中option的索引
alert($("#sl ").get(0).selectedIndex);
alert($('#sl').prop('selectedIndex'));
alert($('#sl').find('option:selected').index());
alert($('#sl').prop('selectedIndex'));
alert($('#sl option').index($('#sl option:selected')));

//設(shè)置select 選中的value:
$("#sl ").attr("value", "1");
$("#sl ").val("1");
$("#sl ").get(0).value = 1;

//設(shè)置select的option選項(xiàng)
$("#sl").append("<option value='4'>第四個(gè) 添加的</option>"); //添加一項(xiàng)option
$("#sl").prepend("<option value='-1'>最前邊 添加的</option>"); //在前面插入一項(xiàng)option
$("#sl option:last").remove(); //刪除索引值最大的option
$("#sl option[value='2']").remove(); //刪除值valuie為2的option
$("#sl option[index='0']").remove(); //刪除索引值為0的option  jquery1.6以后就不支持這樣刪除了
$("#sl option[text='第三個(gè)']").remove(); //刪除text值為4的option jquery 1.2以后就不支持了

//清空select的所有option
$("#sl ").empty();
</script>http://www.2799999.com

感謝各位的閱讀,以上就是“如何獲取select下拉框的選中值”的內(nèi)容了,經(jīng)過本文的學(xué)習(xí)后,相信大家對(duì)如何獲取select下拉框的選中值這一問題有了更深刻的體會(huì),具體使用情況還需要大家實(shí)踐驗(yàn)證。這里是億速云,小編將為大家推送更多相關(guān)知識(shí)點(diǎn)的文章,歡迎關(guān)注!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI