3,302 views

使用函数获取URL中参数的值

<script type="text/javascript">
	//getQueryVariable函数获取URL中参数的值
	function getQueryVariable(variable)
	{
		   var query = window.location.search.substring(1);
		   var vars = query.split("&");
		   for (var i=0;i<vars.length;i++) {
				   var pair = vars[i].split("=");
				   if(pair[0] == variable){return pair[1];}
		   }
		   return(false);
	}
	//在ID为youName的元素中显示获取的值,也可以添加点击函数获取例如:onclick="myFunction()"
	//function myFunction() {
	//	  document.getElementById("youName").innerHTML =getQueryVariable("tex");
	//	}
	document.getElementById("youName").innerHTML =getQueryVariable("tex");
</script>

JS下拉列表的联动

<head>
    <meta charset="UTF-8">
    <title>联动</title>
    <script type="text/javascript">
        function fn() {
            var select = document.getElementById("type");
            var c = select.value;
            var area = document.getElementById("area");
            switch (c) {
                case "fruit":
                    area.innerHTML = "<option>苹果</option><option>橘子</option><option>梨</option><option>桃</option>";
                    break;
                case "animal":
                    area.innerHTML = "<option>老虎</option><option>狮子</option><option>大象</option><option>猴子</option>";
                    break;
                case "plant":
                    area.innerHTML = "<option>月季</option><option>菊花</option><option>郁金香</option><option>松树</option>";
                    break;
                default:
                    alert("error");
            }
        };
    </script>
</head>

<body>
    <select id="type" onchange="fn()">
        <option value="fruit">水果</option>
        <option value="animal">动物</option>
        <option value="plant">植物</option>
    </select>
    <select id="area">
        <option>苹果</option>
        <option>橘子</option>
        <option>梨</option>
        <option>桃</option>
    </select>
</body>