DIV CSS 佈局教程網

jQuery 操作表單元素大全:select、radio、checkbox、text
編輯:JQuery特效代碼     

先從一個實際需求開始

常見的互動是:下拉選單選了 A,就展開 A 的區塊、收起 B 的區塊。

$(document).ready(function () {
    $("#selectMenu").bind("change", function () {
        if ($(this).val() == "pro1") {
            $("#pro1").slideDown();
            $("#pro2").slideUp();
        } else if ($(this).val() == "pro2") {
            $("#pro2").slideDown();
            $("#pro1").slideUp();
        }
    });
});
<select id="selectMenu">
  <option value="">Please select product below</option>
  <option value="pro1">Product 1</option>
  <option value="pro2">Product 2</option>
</select>

一、select 的基本操作

// 綁定 change 事件
$("#select_id").change(function () { /* code... */ });

var checkValue = $("#select_id").val();                                  // 取得選中項的 value
var checkValue = $('select[name="country"]').val();                      // 用 name 定位後取值
var checkValue = $("#select_id").val().join(",");                        // multiple 時取得多選值
var checkText  = $("#select_id").find("option:selected").text();         // 取得選中項的文字
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").empty();                                                 // 清空
$("#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

二、radio 的基本操作

var item = $('input[name=items]:checked').val();          // 取得選中項的值
$('input[name=items]').get(1).checked = true;             // 用原生方式選中第二個
$("input[type=radio][value=2]").attr("checked", "checked"); // 選中 value=2

三、checkbox 的基本操作

$("input[type=checkbox]:checked").val();   // 第一個被勾選項的值
$("input[type=checkbox]:checked").each(function () {
    alert($(this).val());                  // 勾選通常多個,逐一輸出
});
$("#chk1").attr("checked", '');            // 取消勾選
$("#chk2").attr("checked", true);          // 勾選
if ($("#chk1").attr('checked') == undefined) { /* 未勾選 */ }

四、text 與 textarea 的基本操作

$("#txt").attr("value");        // 取值
$("#txt").attr("value", '');    // 清空
$("#txt").attr("value", '11');  // 填入內容
$("#txt").val();                // 更推薦的寫法

幾個版本相關的重要提醒

上面 radio 與 checkbox 的範例沿用了 attr(),這是 jQuery 1.6 之前的寫法。在 1.6 之後,checked 與 selected 這類會變動的狀態請改用 prop(),否則在 Chrome、Firefox 等現代瀏覽器上會出現「第一次有效、之後失效」的問題:

$("input[type=checkbox]").prop("checked", true);
if ($("#chk1").prop("checked")) { /* ... */ }

另外,$('select[name=country]') 這種寫法在 jQuery 1.3 之後已被標記為不建議使用,請加上引號寫成 $('select[name="country"]'),在 CSS 選擇器語法中這才是標準格式。

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