加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

javascript当中options的用法

(2019-10-06 15:36:32)
标签:

javascript

mark-to-win

newoption

selectedindex

分类: javascript

6.options
马克-to-win:选择列表

例 6.1(SelectOptionAddIEFF.html)

!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"
HTML
head
    meta http-equiv="content-type" content="text/html; charset=utf-8"/
    TITLE
    /TITLE
    SCRIPT LANGUAGE="JavaScript"
        !--

       
        var arr = new Array(
                new Array("河北", "邯郸","石家庄"),
                new Array("山东", "济南", "枣庄", "威海"),
                new Array("河南", "开封", "郑州", "洛阳",  "南洋")
        );

        function setProvinces()
        {
           
            for (var i = 0; i < arr.length; i++)
            
                document.getElementByIdx_x_x("province").options[i + 1] = new Option(arr[i][0], arr[i][0]);
            }
        }
        function setCity(i)
         alert(i+" "+document.getElementByIdx_x_x("province").selectedIndex+document.getElementByIdx_x_x("province").value+document.getElementByIdx_x_x("province").options[document.getElementByIdx_x_x("province").selectedIndex].text+document.getElementByIdx_x_x("province").options[document.getElementByIdx_x_x("province").selectedIndex].value);
            document.getElementByIdx_x_x("city").options.length = 1;//reset city
            for (var j = 1; j < arr[i - 1].length; j++)
            {
                opt = new Option(arr[i - 1][j], arr[i - 1][j]);
               
                //         document.getElementByIdx_x_x("city").options[j] = new Option(arr[i - 1][j], arr[i - 1][j]);
                document.getElementByIdx_x_x("city").add(opt,document.getElementByIdx_x_x("city").options.length);//第二个参数指定元素放置所在的索引号
            }
//            objSelect.add(objOption, objSelect.selectedIndex);
        }
        //--
    /SCRIPT
/HEAD

BODY onload="setProvinces()"
!--Select.selectedIndex (Property)
qixy: through experiment: Select.selectedIndex is 0-based.但是第0项是“请选择省份-- ”
--
select name="select" id="province" onchange="setCity(this.selectedIndex);"
    option value="0"请选择省份--/option
/select
select name="select2" id="city"
    option value="0"请选择城市--/option
/select
/BODY
/HTML

更多请见:http://www.mark-to-win.com/tutorial/js_6_newOption.html

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有