下面是html通過js代碼調用select的各種用法總結的相關內容,文章教程主要講述與select 相關的一些技術與知識,更多的內容歡迎大家訪問http://www.49028c.com,獲取更多最新教程,下面是教程講解:
這篇文章給大家介紹html通過js代碼調用select的各種用法總結
1)select的隱藏和顯示:
$("selectList").style.display=$("selectList").style.display=="block"?"none":"block";
2)select 可用不可用:
<select disabled="value"> document.getElementById("LevelDropList").disabled = "value"; 不可用 document.getElementById("LevelDropList").disabled = "value"; 可用
<html>< head>< SCRIPT LANGUAGE="JavaScript">< !--//oSelect 列表的底部添加了一個新選項function onload(){var oOption = document.createElement("OPTION");oOption.text="Ferrari";oOption.value="4";oSelect.add(oOption);}
function fnChange(){oData.value+=oCars.options[oCars.selectedIndex].text + " ";}//-->< /SCRIPT>< /head><body onload="onload()">< !--手工添加一個Select-->< !--1 uses the SELECT element to create a drop-down list box--><SELECT NAME="Cats" SIZE="1">< OPTION VALUE="1">Calico< OPTION VALUE="2">Tortie< OPTION VALUE="3" SELECTED>Siamese< /SELECT><P>< !--2 select 元素創建了多項選擇列表框,方法是設置了 SIZE 和 MULTIPLE 標簽屬性。要獲得多項選擇列表框的選中選項,則須遍歷 options 集合并檢查 SELECTED 是否被設為 true。--><SELECT ID="oSelect" NAME="Cars" SIZE="3" MULTIPLE> <!--此處設置了3和multiple--><OPTION VALUE="1" SELECTED>BMW< OPTION VALUE="2">Porsche< OPTION VALUE="3" SELECTED>Mercedes< /SELECT><P>< !--3 以下演示Option的用法--><SELECT ID="oCars" SIZE="1" onchange="fnChange()">< OPTION VALUE="1">寶馬<OPTION VALUE="2">保時捷<OPTION VALUE="3" SELECTED>大奔< /SELECT><P>< TEXTAREA ID="oData"></TEXTAREA>< /body></html>
附:一些Select的技巧
1.動態創建select
function createSelect(){var mySelect = document.createElement("select");mySelect.id = "mySelect"; document.body.appendChild(mySelect);}
2.添加選項option
function addOption(){//根據id查找對象,var obj=document.getElementById('mySelect');//添加一個選項obj.add(new Option("文本","值"));}
3.刪除所有選項option
function removeAll(){var obj=document.getElementById('mySelect');obj.options.length=0;}
4.刪除一個選項option
function removeOne(){var obj=document.getElementById('mySelect');//index,要刪除選項的序號,這里取當前選中選項的序號var index=obj.selectedIndex;obj.options.remove(index); }
5.獲得選項option的值
var obj=document.getElementById('mySelect');var index=obj.selectedIndex; //序號,取當前選中選項的序號var val = obj.options[index].value;
6.獲得選項option的文本
var obj=document.getElementById('mySelect');var index=obj.selectedIndex; //序號,取當前選中選項的序號var val = obj.options[index].text;
7.修改選項option
var obj=document.getElementById('mySelect');var index=obj.selectedIndex; //序號,取當前選中選項的序號var val = obj.options[index]=new Option("新文本","新值");
8.刪除select
function removeSelect(){var mySelect = document.getElementById("mySelect");mySelect.parentNode.removeChild(mySelect);}
9.設置select optin被中
function removeSelect(){// 向辦件人員下拉列表動態添加員工for ( var i = 0; i < json.length; i++) {var newOption = new Option(json[i].empname, json[i].empid, i);//向辦件人員下拉列表添加員工信息objDeal.options.add(newOption);//客戶業務員的Id不為空if(empbyDealEmpId!="" empbyDealEmpId!=0){//員工id等于下拉列表中的值,則下拉列表被選中if(empbyDealEmpId==objDeal.options[i].value){//判斷此下拉列表被選中objDeal.options[i].selected=true;
End. 教程到這里講完了,閱讀是否有所收獲呢?本站還提供有select 相關的內容,歡迎繼續閱讀。
新聞熱點
疑難解答