html中select標(biāo)簽單選多選用法詳解

字號(hào):


    select 元素可創(chuàng)建單選或多選菜單。當(dāng)提交表單時(shí),瀏覽器會(huì)提交選定的項(xiàng)目,或者收集用逗號(hào)分隔的多個(gè)選項(xiàng),將其合成一個(gè)單獨(dú)的參數(shù)列表,并且在將 <select> 表單數(shù)據(jù)提交給服務(wù)器時(shí)包括 name 屬性。
    一、基本用法:
    代碼如下:
    <select>
    <option value =volvo>volvo</option>
    <option value =saab>saab</option>
    <option value=opel>opel</option>
    <option value=audi>audi</option>
    </select>
    其中,</option>標(biāo)簽可以省掉,在頁面中用法
    代碼如下:
    <select name=studycenter id=studycenter size=1>
    <option value=0>全部
    <option value=1>湖北電大網(wǎng)絡(luò)學(xué)習(xí)中心
    <option value=2>成都師范學(xué)院網(wǎng)絡(luò)學(xué)習(xí)中心
    <option value=3>武漢職業(yè)技術(shù)學(xué)院網(wǎng)絡(luò)學(xué)習(xí)中心
    </select>
    二、select元素還可以多選,看如下代碼:
    代碼如下:
    //有multiple屬性,則可以多選
    <select name= “education” id=”education” multiple=”multiple”>
    <option value=”1”>高中</option>
    <option value=”2”>大學(xué)</option>
    <option value=”3”>博士</option>
    </select>
    //下面沒有multiple屬性 , 只顯示一條,不能多選
    <select name= “education” id=”education” >
    <option value=”1”>高中</option>
    <option value=”2”>大學(xué)</option>
    <option value=”3”>博士</option>
    </select>
    //下面是設(shè)置了size屬性的情況 , 如果size = 3 那么就顯示三條數(shù)據(jù),注意不能多選的。
    <select name=education id=education size='3'>
    <option value=0>小學(xué)</option>
    <option value=1>初中</option>
    <option value=2>高中</option>
    <option value=3>中專</option>
    <option value=4>大專</option>
    <option value=5>本科</option>
    <option value=6>研究生</option>
    <option value=7>博士</option>
    <option value=8>博士后</option>
    <option selected>請選擇</option>
    </select>
    三、多選select組件涉及的所有常用操作:
    1. 判斷select選項(xiàng)中是否存在指定值的item
    代碼如下:
    @param objselectid 將要驗(yàn)證的目標(biāo)select組件的id
    @param objitemvalue 將要驗(yàn)證是否存在的值
    function isselectitemexit(objselectid,objitemvalue) {
    var objselect = document.getelementbyid(objselectid);
    var isexit = false;
    if (null != objselect && typeof(objselect) != undefined) {
    for(var i=0;i<objselect.options.length;i++) {
    if(objselect.options[i].value == objitemvalue) {
    isexit = true;
    break;
    }
    }
    }
    return isexit;
    }
    2.向select選項(xiàng)中加入一個(gè)item
    代碼如下:
    @param objselectid 將要加入item的目標(biāo)select組件的id
    @param objitemtext 將要加入的item顯示的內(nèi)容
    @param objitemvalue 將要加入的item的值
    function addoneitemtoselect(objselectid,objitemtext,objitemvalue) {
    var objselect = document.getelementbyid(objselectid);
    if (null != objselect && typeof(objselect) != undefined) {
    //判斷是否該值的item已經(jīng)在select中存在
    if(isselectitemexit(objselectid,objitemvalue)) {
    $.messager.alert('提示消息','該值的選項(xiàng)已經(jīng)存在!','info');
    } else {
    var varitem = new option(objitemtext,objitemvalue);
    objselect.options.add(varitem);
    }
    }
    }
    3.從select選項(xiàng)中刪除選中的項(xiàng),支持多選多刪
    代碼如下:
    @param objselectid 將要進(jìn)行刪除的目標(biāo)select組件id
    function removeselectitemsfromselect(objselectid) {
    var objselect = document.getelementbyid(objselectid);
    var delnum = 0;
    if (null != objselect && typeof(objselect) != undefined) {
    for(var i=0;i<objselect.options.length;i=i+1) {
    if(objselect.options[i].selected) {
    objselect.options.remove(i);
    delnum = delnum + 1;
    i = i - 1;
    }
    }
    if (delnum <= 0 ) {
    $.messager.alert('提示消息','請選擇你要?jiǎng)h除的選項(xiàng)!','info');
    } else {
    $.messager.alert('提示消息','成功刪除了'+delnum+'個(gè)選項(xiàng)!','info');
    }
    }
    }
    4.從select選項(xiàng)中按指定的值刪除一個(gè)item
    代碼如下:
    @param objselectid 將要驗(yàn)證的目標(biāo)select組件的id
    @param objitemvalue 將要驗(yàn)證是否存在的值
    function removeitemfromselectbyitemvalue(objselectid,objitemvalue) {
    var objselect = document.getelementbyid(objselectid);
    if (null != objselect && typeof(objselect) != undefined) {
    //判斷是否存在
    if(isselectitemexit(objselect,objitemvalue)) {
    for(var i=0;i<objselect.options.length;i++) {
    if(objselect.options[i].value == objitemvalue) {
    objselect.options.remove(i);
    break;
    }
    }
    $.messager.alert('提示消息','成功刪除!','info');
    } else {
    $.messager.alert('提示消息','不存在指定值的選項(xiàng)!','info');
    }
    }
    }
    5.清空select中的所有選項(xiàng)
    代碼如下:
    @param objselectid 將要進(jìn)行清空的目標(biāo)select組件id
    function clearselect(objselectid) {
    var objselect = document.getelementbyid(objselectid);
    if (null != objselect && typeof(objselect) != undefined) {
    for(var i=0;i<objselect.options.length;) {
    objselect.options.remove(i);
    }
    }
    }
    6. 獲取select中的所有item,并且組裝所有的值為一個(gè)字符串,值與值之間用逗號(hào)隔開
    代碼如下:
    @param objselectid 目標(biāo)select組件id
    @return select中所有item的值,值與值之間用逗號(hào)隔開
    function getallitemvaluesbystring(objselectid) {
    var selectitemsvaluesstr = ;
    var objselect = document.getelementbyid(objselectid);
    if (null != objselect && typeof(objselect) != undefined) {
    var length = objselect.options.length
    for(var i = 0; i < length; i = i + 1) {
    if (0 == i) {
    selectitemsvaluesstr = objselect.options[i].value;
    } else {
    selectitemsvaluesstr = selectitemsvaluesstr + , + objselect.options[i].value;
    }
    }
    }
    return selectitemsvaluesstr;
    }
    7. 將一個(gè)select中的所有選中的選項(xiàng)移到另一個(gè)select中去
    代碼如下:
    @param fromobjselectid 移動(dòng)item的原select組件id
    @param toobjectselectid 移動(dòng)item將要進(jìn)入的目標(biāo)select組件id
    function moveallselectedtoanotherselectobject(fromobjselectid, toobjectselectid) {
    var objselect = document.getelementbyid(fromobjselectid);
    var delnum = 0;
    if (null != objselect && typeof(objselect) != undefined) {
    for(var i=0;i<objselect.options.length;i=i+1) {
    if(objselect.options[i].selected) {
    addoneitemtoselect(toobjectselectid,objselect.options[i].text,objselect.options[i].value)
    objselect.options.remove(i);
    i = i - 1;
    }
    }
    }
    }
    8. 將一個(gè)select中的所有選項(xiàng)移到另一個(gè)select中去
    代碼如下:
    @param fromobjselectid 移動(dòng)item的原select組件id
    @param toobjectselectid 移動(dòng)item將要進(jìn)入的目標(biāo)select組件id
    function movealltoanotherselectobject(fromobjselectid, toobjectselectid) {
    var objselect = document.getelementbyid(fromobjselectid);
    if (null != objselect) {
    for(var i=0;i<objselect.options.length;i=i+1) {
    addoneitemtoselect(toobjectselectid,objselect.options[i].text,objselect.options[i].value)
    objselect.options.remove(i);
    i = i - 1;
    }
    }
    }