$("#select_id").change(function(){ /* code... */ }); // 綁定 change 事件
var checkText = $("#select_id").find("option:selected").text(); // 選中項文字
var checkValue = $("#select_id").val(); // 選中項值
var checkIndex = $("#select_id").get(0).selectedIndex; // 選中項索引
var maxIndex = $("#select_id option:last").attr("index"); // 最大索引
$("#select_id").get(0).selectedIndex = 1; // 選中索引 1
$("#select_id").val(4); // 選中 value=4
$("#select_id option[text='jQuery']").attr("selected", true); // 選中文本為 jQuery 者
$("#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(); // 刪 text=4
$("#SelectID").remove(); // 清空整個 select
$("input[name='chk_list']").attr("checked"); // 讀取狀態
$("input[name='chk_list']").attr("checked", true); // 全部設為選中
$("#img_1").attr("src","test.jpg"); // 設 img 的 src
$("#img_1").attr("src"); // 讀 img 的 src
<script type="text/javascript">
var arrChk = $("input[name='chk_list'][checked]");
$(arrChk).each(function(){
window.alert(this.value);
});
</script>
$("#checkboxID").value // 取值
$("input[type='checkbox']").eq(n).attr("checked") // 指定項的狀態
$("input[type='checkbox']").eq(1).attr("checked") // 設定第一項為選中
$("input[type='checkbox']").remove() // 移除所有 checkbox
$(document).ready(function() {
var check = $("input[type='checkbox']");
check.each(function(n) {
check.eq(n).bind("click", function() {
if (check.eq(n).attr("checked") != false) {
alert(check.eq(n).val());
} else {
alert(check.eq(n).attr("checked"));
}
});
});
});
$("input[type='radio']:checked").val(); // 取得選中項的值
$("input[type='radio']").eq(1).attr("checked", true); // 第二項設為選中
$("input[type='radio'][value='值']").attr("checked", true);
$("input[type='radio'][@name='rdoTest2']").eq(0).attr("checked", true); // 多組 radio 時指定 name
下面這個範例把三類控制項放在一起,包含必填檢查、單選、多選、下拉與全選/反選:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript">
$(function(){
// 用戶名即時檢查
$('#uname').blur(function(){
$('#err').html($('#uname').val() == '' ? '*' : '');
});
$('#see').click(function(){
if ($('#uname').val() == '') { $('#err').html('*'); return; }
$('#err').html('');
$('#name').html($('#uname').val());
$('#xingbie').html($("[name='aa']:checked").val()); // 單選
var str = "";
$("[name='bbb']:checked").each(function(){ // 多選,多值串接
str += $(this).val() + " ";
});
$('#shengfen').html(str);
$('#nianfen').html($('#nf').find('option:selected').val()); // 下拉取 value
});
$('#quan').click(function(){ $("[name='ccc']").attr('checked','true'); }); // 全選
$('#buxuan').click(function(){ $("[name='ccc']").removeAttr('checked'); }); // 全不選
$('#fan').click(function(){ // 反選
$("[name='ccc']").each(function(){
if ($(this).attr("checked")) { $(this).removeAttr("checked"); }
else { $(this).attr("checked",'true'); }
});
});
$('#kan').click(function(){
var ss = "";
$("[name='ccc']:checked").each(function(){ ss += $(this).val() + " "; });
$('#seee').html(ss);
});
});
</script>
反選的邏輯值得留意:它同時讀取並修改 checked 屬性,在 jQuery 1.6 之後 attr 與 prop 分離,這種讀寫混用的寫法會出錯,應改為 $(this).prop("checked", !$(this).prop("checked"))。同樣地,全選與取消勾選也建議改用 .prop("checked", true/false)。
另外,範例中 $('#nf').find('option:selected').val() 取的是 value,若要顯示給使用者看的文字則要改用 .text()——兩者差一個字,但呈現結果完全不同,是常見的混淆點。
執行後請重新整理瀏覽器才能看到效果。