js selected属性应用

司寇山
2023-12-01
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script>

        function selectOneLeftToRight() {
           var leftSelect=document.getElementById("leftSelect");
           var options=leftSelect.options;
           var rightSelect=document.getElementById("rightSelect");
           var optionss=rightSelect.options;
           for(var i=0;i<options.length;i++){
               var option1=options[i];
               if (option1.selected) {
                   rightSelect.appendChild(option1);
               }
           }
        }
        function selectAllLeftToRight()
        {
            var selectLeft = document.getElementById("leftSelect");
            var selectRight  = document.getElementById("rightSelect");
            var optionsLeft = selectLeft.options;
            for(var i = 0; i < optionsLeft.length; i++)
            {
                selectRight.appendChild(optionsLeft[i]);
                i--;
            }
        }


        function selectOneRightToLeft()
        {
            var selectLeft = document.getElementById("leftSelect");
            var selectRight  = document.getElementById("rightSelect");
            var optionsRight = selectRight.options;
            for(var i = 0; i < optionsRight.length; i++)
        {
                if(optionsRight[i].selected == true)
                {
                    selectLeft.appendChild(optionsRight[i]);
                    // i--;
                }
            }
        }
        function selectAllRightToLeft()
        {
            var selectLeft = document.getElementById("leftSelect");
            var selectRight  = document.getElementById("rightSelect");
            var optionsRight = selectRight.options;
            for(var i = 0; i < optionsRight.length; i++)
            {
                selectLeft.appendChild(optionsRight[i]);
                i--;
            }
        }
    </script>
</head>
<body>
<table width="400px" border="1px">
    <tr>
        <td>分类名称</td>
        <td><input type="text" value="这是手机数码"/></td>
    </tr>
    <tr>
        <td>分类商品</td>
        <td>
            <div style="float:left">
                已有商品<br/>
                <select multiple="multiple" id="leftSelect" ondblclick="selectOneLeftToRight()">
                    <option>华为</option>
                    <option>小米</option>
                    <option>诺基亚</option>
                </select>
                <br/>
                <a href="#" onclick="selectOneLeftToRight()">&gt;&gt;</a><br/>
                <a href="#" onclick="selectAllLeftToRight()">&gt;&gt;&gt;</a>
            </div>
            <div style="float:right">
                未选商品<br/>
                <select multiple="multiple" id="rightSelect" ondblclick="selectOneRightToLeft()" >
                    <option>苹果</option>
                    <option>三星</option>
                    <option>oppo</option>
                </select>
                <br/>
                <a href="#" onclick="selectOneRightToLeft()">&lt;&lt;</a><br/>
                <a href="#" onclick="selectAllRightToLeft()">&lt;&lt;&lt;</a>
            </div>
        </td>
    </tr>
</table>
</body>
</html>
 类似资料: