码迷,mamicode.com
首页 > 其他好文 > 详细

选择框脚本_添加/删除选项 P434

时间:2020-08-02 16:17:01      阅读:107      评论:0      收藏:0      [点我收藏+]

标签:china   script   length   mit   ati   信息   sel   field   删除   

 

<!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 + "\nSelected text: " + selectedOptions[i].text + "\nSelected value: " + selectedOptions[i].value + "\n\n";
                }
                
                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

标签:china   script   length   mit   ati   信息   sel   field   删除   

原文地址:https://www.cnblogs.com/jiunie/p/13418756.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!