选择框脚本_添加/删除选项 P434
Posted jiunie
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了选择框脚本_添加/删除选项 P434相关的知识,希望对你有一定的参考价值。
<!DOCTYPE html> <html> <head> <title>Selectbox Example</title> <script type="text/javascript" src="EventUtil.js"></script> </head> <body> <form method="post" action="javascript:alert(‘Form submitted!‘)" id="myForm"> <div> <label for="selLocation">Where do you want to live?</label> <select name="location" id="selLocation" size="5" multiple> <option value="Sunnyvale, CA">Sunnyvale</option> <option value="Los Angeles, CA">Los Angeles</option> <option value="Mountain View, CA">Mountain View</option> <option value="">China</option> <option>Australia</option> </select> </div> <fieldset> <legend>Add a location</legend> <label for="txtText">What should the text be?文本应该是什么?</label> <input type="text" id="txtText" value=""><br> <label for="txtValue">What should the value be?值应该是什么?</label> <input type="text" id="txtValue" value=""><br> <input type="button" value="使用DOM添加" id="btnAdd1"> <input type="button" value="使用Add()方法添加" id="btnAdd2"> </fieldset> <fieldset> <legend>Remove a location</legend> <input type="button" value="使用DOM删除" id="btnRemove1"> <input type="button" value="使用remove()方法删除" id="btnRemove2"> <input type="button" value="使用null删除" id="btnRemove3"> </fieldset> <input type="button" value="获取选中项信息" id="btnSelected"> </form> <script type="text/javascript"> (function(){ function getSelectedOptions(selectbox){ var result = new Array(); var option = null; for (var i=0, len=selectbox.options.length; i < len; i++){ option = selectbox.options[i]; if (option.selected){ result.push(option); } } return result; } var btn1 = document.getElementById("btnAdd1"); var btn2 = document.getElementById("btnAdd2"); var btn3 = document.getElementById("btnSelected"); var btn4 = document.getElementById("btnRemove1"); var btn5 = document.getElementById("btnRemove2"); var btn6 = document.getElementById("btnRemove3"); EventUtil.addHandler(btn1, "click", function(event){ var selectbox = document.getElementById("selLocation"); var textTextbox = document.getElementById("txtText"); var valueTextbox = document.getElementById("txtValue"); var newOption = document.createElement("option"); newOption.appendChild(document.createTextNode(textTextbox.value)); newOption.setAttribute("value", valueTextbox.value); selectbox.appendChild(newOption); }); EventUtil.addHandler(btn2, "click", function(event){ var selectbox = document.getElementById("selLocation"); var textTextbox = document.getElementById("txtText"); var valueTextbox = document.getElementById("txtValue"); var newOption = new Option(textTextbox.value, valueTextbox.value); selectbox.appendChild(newOption); //selectbox.add(newOption, undefined); }); EventUtil.addHandler(btn3, "click", function(event){ var selectbox = document.getElementById("selLocation"); var selectedOptions = getSelectedOptions(selectbox); var message = ""; for (var i=0, len=selectedOptions.length; i < len; i++){ message += "Selected index: " + selectedOptions[i].index + " Selected text: " + selectedOptions[i].text + " Selected value: " + selectedOptions[i].value + " "; } alert(message); }); EventUtil.addHandler(btn4, "click", function(event){ var selectbox = document.getElementById("selLocation"); selectbox.removeChild(selectbox.options[0]); }); EventUtil.addHandler(btn5, "click", function(event){ var selectbox = document.getElementById("selLocation"); selectbox.remove(0); }); EventUtil.addHandler(btn6, "click", function(event){ var selectbox = document.getElementById("selLocation"); selectbox.options[0] = null; }); })(); </script> </body> </html>
以上是关于选择框脚本_添加/删除选项 P434的主要内容,如果未能解决你的问题,请参考以下文章