DIV CSS 佈局教程網

您现在的位置: DIV+CSS佈局教程網 >> 網頁腳本 >  >> HTML基礎知識 >> HTML和Xhtml
HTML select 標籤單選與多選用法詳解
編輯:HTML和Xhtml     

select 元素可建立單選或多選的清單。表單送出時,瀏覽器會把選定項目的值以 name 為鍵送出;多選時會收集多個值組成參數列表。

一、基本用法

<select name="car">
  <option value="volvo">Volvo</option>
  <option value="saab">Saab</option>
  <option value="opel">Opel</option>
  <option value="audi">Audi</option>
</select>

HTML5 允許省略 </option> 結束標籤,但明確寫上更好讀:

<select name="studyCenter" id="studyCenter">
  <option value="0">全部</option>
  <option value="1">湖北電大網絡學習中心</option>
  <option value="2">成都師范學院網絡學習中心</option>
</select>

二、單選與多選的差別

<!-- 有 multiple:可以多選 -->
<select name="education" id="edu1" multiple>
  <option value="1">高中</option>
  <option value="2">大學</option>
  <option value="3">博士</option>
</select>

<!-- 沒有 multiple:只能選一項 -->
<select name="education" id="edu2">
  <option value="1">高中</option>
  <option value="2">大學</option>
</select>

<!-- 有 size:直接顯示指定行數,但仍不能多選 -->
<select name="education" id="edu3" size="3">
  <option value="0">小學</option>
  <option value="1">初中</option>
  <option value="2">高中</option>
  <option value="3">專科</option>
</select>

要留意:size 只影響顯示行數,不會讓單選變成多選。另外 multiple 的清單在作業系統層級需要按住 Ctrl(Mac 為 Cmd)才能多選,這對一般使用者並不直覺,是個已知的可ability問題。實務上若要多選,用一組 checkbox 往往比 multiple select 友善得多。

三、常用操作

判斷某個值是否存在:

function isSelectItemExit(selectId, itemValue) {
  var sel = document.getElementById(selectId);
  if (!sel) return false;
  for (var i = 0; i < sel.options.length; i++) {
    if (sel.options[i].value == itemValue) return true;
  }
  return false;
}

加入一項:

function addOneItemToSelect(selectId, text, value) {
  var sel = document.getElementById(selectId);
  if (!sel) return;
  if (isSelectItemExit(selectId, value)) {
    alert('該值的選項已經存在!');
    return;
  }
  sel.options.add(new Option(text, value));
}

刪除所有選中項(支援多選多刪):

function removeSelectedItems(selectId) {
  var sel = document.getElementById(selectId);
  if (!sel) return 0;
  var removed = 0;
  for (var i = sel.options.length - 1; i >= 0; i--) {
    if (sel.options[i].selected) {
      sel.options.remove(i);
      removed++;
    }
  }
  return removed;
}

這裡有個重點:迴圈要從後往前跑。若從前往後,刪掉一個之後索引會錯位而跳過下一項。原範例用 i = i - 1 補救也可以,但倒序迴圈更直觀、不易出錯。

依值刪除:

function removeItemByValue(selectId, itemValue) {
  var sel = document.getElementById(selectId);
  if (!sel) return false;
  for (var i = 0; i < sel.options.length; i++) {
    if (sel.options[i].value == itemValue) {
      sel.options.remove(i);
      return true;
    }
  }
  return false;
}

清空:

function clearSelect(selectId) {
  var sel = document.getElementById(selectId);
  if (sel) sel.options.length = 0;   // 一行搞定
}

設定 options.length = 0 比迴圈逐一 remove 簡潔得多。

把所有值組成人字串:

function getAllValues(selectId) {
  var sel = document.getElementById(selectId);
  if (!sel) return "";
  return Array.prototype.map
    .call(sel.options, function(o) { return o.value; })
    .join(",");
}

把選中項移到另一個 select:

function moveSelected(fromId, toId) {
  var from = document.getElementById(fromId);
  var to = document.getElementById(toId);
  if (!from || !to) return;
  for (var i = from.options.length - 1; i >= 0; i--) {
    if (from.options[i].selected) {
      to.options.add(new Option(from.options[i].text, from.options[i].value));
      from.options.remove(i);
    }
  }
}

全部搬移只要把條件改成無條件即可,或者直接:

function moveAll(fromId, toId) {
  var from = document.getElementById(fromId);
  var to = document.getElementById(toId);
  if (!from || !to) return;
  while (from.options.length) {
    to.options.add(from.options[0]);
  }
}

幾個實務提醒

預設選項:用 <option selected> 指定,或用 sel.selectedIndex = 0。

placeholder 選項:想做「請選擇……」的引導項,給它一個空值並加上 disabled 與 selected:

<option value="" disabled selected>請選擇</option>

這樣使用者無法回頭選它,後端也能用空值判斷「未選擇」。

後端收值:多選時 PHP 需要把 name 寫成 education[] 才會收到陣列,否則只會拿到最後一項。這是常見的踩坑點。

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