[轉(zhuǎn)帖]js 操作select和option常見用法
當前位置:點晴教程→知識管理交流
→『 技術(shù)文檔交流 』
:js 操作select和option常見用法 js 操作select和option常見用法1、獲取選中select的value和text,html <select id="myselect"> <option value="1">one</option> <option value="2">two</option> <option value="3">three</option> </select> 通過以下script代碼s來獲取選中的value和text $("#myselect").val(); //獲取選中記錄的value值$("#myselect option:selected").text(); //獲取選中記錄的text值 2、運用new Option("文本","值")方法添加選項option var obj = document.getElementById("myselect");obj.add(new Option("4","4")); 3、刪除所有選項option var obj = document.getElementById("myselect");obj.options.length = 0; 4、刪除選中選項option var obj = document.getElementById("myselect");var index = obj.selectedIndex;obj.options.remove(index); 5、修改選中選項option var obj = document.getElementById("myselect");var index = obj.selectedIndex; obj.options[index] = new Option("three",3); //更改對應(yīng)的值obj.options[index].selected = true; //保持選中狀態(tài) 6、刪除select var obj = document.getElementById("myselect"); obj.parentNode.removeChild(obj); //移除當前對象 7、select選擇的響應(yīng)事件 $("#myselect").change(function(){ //添加所需要執(zhí)行的操作代碼}) 1.動態(tài)創(chuàng)建select function createselect(){var myselect = document.createElement_x("select"); myselect.id = "myselect"; document.body.appendChild(myselect); } 2.添加選項option function addOption(){//根據(jù)id查找對象,var obj=document.getElementByIdx_x('myselect');//添加一個選項obj.add(new Option("文本","值")); //這個只能在IE中有效obj.options.add(new Option("text","value")); //這個兼容IE與firefox} 3.刪除所有選項option function removeAll(){var obj=document.getElementByIdx_x('myselect'); obj.options.length=0; } 4.刪除一個選項option 5.獲得選項option的值 var obj=document.getElementByIdx_x('myselect');var index=obj.selectedIndex; //序號,取當前選中選項的序號var val = obj.options[index].value; 6.獲得選項option的文本 var obj=document.getElementByIdx_x('myselect');var index=obj.selectedIndex; //序號,取當前選中選項的序號var val = obj.options[index].text;
7.修改選項option var obj=document.getElementByIdx_x('myselect');var index=obj.selectedIndex; //序號,取當前選中選項的序號var val = obj.options[index]=new Option("新文本","新值"); 8.刪除select function removeselect(){var myselect = document.getElementByIdx_x("myselect"); myselect.parentNode.removeChild(myselect); } 整個實例的完整代碼如下: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//ZH-CN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html"> <head> <script language=Javascript>function $(id) {return document.getElementByIdx_x(id) }function show() {var selectObj=$("area")var myOption=document.createElement_x("option") myOption.setAttribute("value","10") myOption.appendChild(document.createTextNode("上海"))var myOption1=document.createElement_x("option") myOption1.setAttribute("value","100") myOption1.appendChild(document.createTextNode("南京")) selectObj.appendChild(myOption) selectObj.appendChild(myOption1) }function choice() {var index=$("area").selectedIndex;var val=$("area").options[index].getAttribute("value")if(val==10) {var i=$("context").childNodes.length-1;var remobj=$("context").childNodes[i]; remobj.removeNode(true)var sh=document.createElement_x("select") sh.add(new Option("浦東新區(qū)","101")) sh.add(new Option("黃浦區(qū)","102")) sh.add(new Option("徐匯區(qū)","103")) sh.add(new Option("普陀區(qū)","104")) $("context").appendChild(sh) }if(val==100) {var i=$("context").childNodes.length-1;var remobj=$("context").childNodes[i]; remobj.removeNode(true)var nj=document.createElement_x("select") nj.add(new Option("玄武區(qū)","201")) nj.add(new Option("白下區(qū)","202")) nj.add(new Option("下關(guān)區(qū)","203")) nj.add(new Option("棲霞區(qū)","204")) $("context").appendChild(nj) } }function calc() {var x=$("context").childNodes.length-1; alert(x) }function remove() {var i=$("context").childNodes.length-1;var remobj=$("context").childNodes[i]; remobj.removeNode(true) }</script> <body> <div id="context"> <select id="area" onchange="choice()"> </select> </div> <input type=button value="顯示" onclick="show()"> <input type=button value="計算結(jié)點" onclick="calc()"> <input type=button value="刪除" onclick="remove()"> </body> </html> 改進版:在select中添加、修改、刪除option元素 1 function watch_ini(){ // 初始 2 for(var i=0; i<arguments.length; i++){ 3 var word = document.createElement_x("OPTION"); 4 word.text = arguments[i]; 5 watch.keywords.add(word); // watch. is form name 6 } 7 } 8 function watch_add(f){ // 增加 9 var word = document.createElement_x("OPTION");10 word.text = f.word.value;11 f.keywords.add(word);12 } 上述 add() 方法只在IE下有效,為兼容FF和Opera,對上述代碼進行了一下改進,改動后代碼如下: 1 function watch_ini(){ // 初始 2 for(var i=0; i<arguments.length; i++){ 3 var oOption=new Option(arguments[i],arguments[i]); 4 document.getElementByIdx_x("Myselect")[i]=oOption; 5 } 6 } 7 function watch_add(f){ // 增加 8 var oOption=new Option(f.word.value,f.word.value); 9 f.keywords[f.keywords.length]=oOption;10 } 整個實例的完整代碼如下: 1 <!doctype html public "-//w3c//dtd html 4.0 transitional//en"> 2 <html> 3 <head> 4 <title>javascript select options text value</title> 5 <meta name="keywords" content="javascript select options text value add modify delete set"> 6 <meta name="description" content="javascript select options text value add modify delete set"> 7 <script language="javascript"> 8 <!-- 9 function watch_ini(){ // 初始10 for(var i=0; i<arguments.length; i++){11 var oOption=new Option(arguments[i],arguments[i]);12 document.getElementByIdx_x("Myselect")[i]=oOption;13 }14 }15 function watch_add(f){ // 增加16 var oOption=new Option(f.word.value,f.word.value);17 f.keywords[f.keywords.length]=oOption;18 }19 function watch_sel(f){ // 編輯20 f.word.value = f.keywords[f.keywords.selectedIndex].text;21 }22 function watch_mod(f){ // 修改23 f.keywords[f.keywords.selectedIndex].text = f.word.value;24 }25 function watch_del(f){ // 刪除26 f.keywords.remove(f.keywords.selectedIndex);27 }28 function watch_set(f){ // 保存29 var set = "";30 for(var i=0; i<f.keywords.length; i++){31 set += f.keywords[i].text + ";";32 }33 confirm(set);34 }35 //-->36 </script>37 </head>38 <body>39 <form name="watch" method="post" action="">40 <select id="Myselect" name="keywords" size="10" onchange="watch_sel(this.form)"></select><br>41 <script language="javascript">42 <!--43 watch_ini("我","你","妳","他","她","它","爾"); // 初始關(guān)鍵詞44 //-->45 </script>46 <input type="text" name="word" /><br />47 <input type="button" value="增加" onclick="watch_add(this.form);" />48 <input type="button" value="修改" onclick="watch_mod(this.form);" />49 <input type="button" value="刪除" onclick="watch_del(this.form);" />50 <input type="button" value="保存" onclick="watch_set(this.form);" />51 </form> 用一個字符串創(chuàng)建一個數(shù)組方法: 1 <script language="javascript"> 2 </body> 3 </html> 4 <script> 5 function spli(){ 6 datastr="2,2,3,5,6,6"; 7 var str= new Array(); 8 str=datastr.split(","); 9 for (i=0;i<str.length ;i++ )10 { document.write(str[i]+"<br/>"); }11 }12 spli();13 </script>
PS:在ie上運行良好,在火狐上調(diào)試出現(xiàn)錯誤,原因如下 該文章在 2023/7/4 16:09:28 編輯過 |
關(guān)鍵字查詢
相關(guān)文章
正在查詢... |