DIV CSS 佈局教程網

jQuery 表單操作小結:單選鈕、多選框、下拉選單的取值的與設值
編輯:關於JavaScript     

一、取值與設值基本語法

var sex = $("input[name='sex']:checked").val(); // 取得一組 radio 中被選中項的值
var item = $("#sel option:selected").text(); // 取得 select 被選中項的文字
var option_num = $('#sel').val(); // 取得 select 被選中項的值
$("#sel")[0].selectedIndex = 1; // 讓 select 的第二個選項成為當前選中值
$("input[name='sex']").get(1).checked = true; // 讓 radio 組的第二個成為選中值

也可以用 each 逐一比對 value 來設定 radio 的預設選取:

$("input[name='sex']").each(function(){
if($(this).val() == s){
$(this).attr("checked", true);
// this.checked = true;
}
});

依 value 設定 select 的預選項

<select name="sel" onchange="bao(this.options[this.options.selectedIndex].value)">
<option value="">請選擇</option>
<option value="1">Item 1</option>
<option value="2">Item 2</option>
<option value="3">Item 3</option>
</select>
<script>
function bao(s)
{
txt.value = s;
// 選擇後讓第一項被選中,這樣就會產生 change 事件
document.all.sel.options[0].selected = true;
}
</script>
<textarea id="txt"></textarea>

二、取得 select 的 Text 與 Value

$("#select_id").change(function(){ /* ... */ }); // 綁定 change 事件
var checkText = $("#select_id").find("option:selected").text(); // 選中項文字
var checkValue = $("#select_id").val(); // 選中項 value
var checkIndex = $("#select_id").get(0).selectedIndex; // 選中項索引
var maxIndex = $("#select_id option:last").attr("index"); // 最大索引

設定 select 的選中項

$("#select_id").get(0).selectedIndex = 1; // 讓索引為 1 的項選中
$("#select_id").val(4); // 讓 value 為 4 的項選中
$("#select_id option[text='jQuery']").attr("selected", true); // 讓文字為 jQuery 的項選中

新增與刪除 option

$("#select_id").append("<option value='Value'>Text</option>"); // 向後追加
$("#select_id").prepend("<option value='0'>請選擇</option>"); // 插入到最前面
$("#select_id option:last").remove(); // 刪除最後一個
$("#select_id option[index='0']").remove(); // 刪除索引為 0 的
$("#select_id option[value='3']").remove(); // 刪除 value 為 3 的
$("#select_id option[text='4']").remove(); // 刪除文字為 4 的

三、完整應用範例

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<title>jQuery common</title>
<script type="text/javascript" src="jquery-1.8.3.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
// 初始化下拉列表——動態添加
var item = ['幼兒園','小學','初中','高中','大學','研究生','博士','碩士'];
var html = "<option value='0'>請選擇</option>";
for (var i = 0; i < item.length; i++){
html += "<option value='" + (i + 1) + "'>" + item[i] + "</option>";
}
$("#grade").empty().append(html);
$("#grade option[value='0']").attr("selected", "selected"); // 預設第一項被選中
})

// 選擇其中一項時觸發
function showIt(){
var selectText = $("#grade option:selected").text(); // 選中的文字
// 這種寫法也可行:$("#grade").find("option:selected").text()
var selectValue = $("#grade").val(); // 選中的 value
var selectIndex = $("#grade").get(0).selectedIndex; // 索引值
var text = '選擇:' + selectText + "\n";
text += 'value值:' + selectValue + "\n";
text += '索引值:' + selectIndex;
$("#text").text(text);
}
</script>
</head>
<body>
<div>
<select name='grade' id='grade' onchange="showIt()"></select>
<p><textarea name='text' id='text' row='30' col='100'></textarea></p>
</div>
</body>
</html>

版本提醒

本文以 jQuery 1.8.3 為例。從 jQuery 1.6 起,attr("checked") 的行為已改變,勾選狀態應改用 .prop("checked", true) 與 .prop("checked") 讀取;.attr() 只反映 HTML 初始狀態。另外 textarea 的屬性是 rows 與 cols(複數),原範例寫成 row、col 不會生效。

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved