请帮助我找到正确的Javascript代码...请

时间:2012-08-23 07:49:42

标签: javascript redirect drop-down-menu onload url-parameters

这里我有两个HTML页面,First_page.html和Second_page.html,

在First_page.html中,我有一个代码,当使用某个URL参数点击链接时,该代码会重定向到某个页面。

First_page.html的代码是这样的。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<a href="Second_page.html?=1">one</a><br />
<a href="Second_page.html?=2">two</a><br />
<a href="Second_page.html?=3">three</a><br />
<a href="Second_page.html?=4">four</a>
</body>
</html>

在Second_page.html中,我有一个代码,用于读取URL参数并根据它更改下拉菜单。

Second_Page.html的代码就像这个

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script type="text/javascript">
function show(choice) { 
  var success = -1;
  for (var i=0; i < document.form1.selecoption.length; i++) {
    if (document.form1.selecoption.options[i].value == choice) 
      success = [i];
  }
  document.form1.selecoption.selectedIndex=success;
}
</script>
</head>

<body onLoad="var choice = location.href.split('?')[1].split('=')[1];show(choice);">
<form name="form1">
  <select name="selecoption">
    <option value="1">ONE</option>
    <option value="2">TWO</option>
    <option value="3">THREE</option>
    <option value="4">FOUR</option>
  </select>
</form>
</body>

现在,我想要的是如何选择两个不同的下拉菜单,即“选择”和“选择”。 Second_page.html中的“selecsecondoption”使用URL参数。请帮帮....

2 个答案:

答案 0 :(得分:0)

试试这个:

<script type="text/javascript">    
    document.getElementById('selecoption').value=String(choice);
</script>

在Second_Page.html

的末尾

答案 1 :(得分:0)

首先,您必须使用正确的URL格式。将值传递给URL需要相应的变量名称。因此,更正后的First_page.html文件应如下所示。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<a href="Second_page.html?selecoption=1">one</a><br />
<a href="Second_page.html?selecoption=2">two</a><br />
<a href="Second_page.html?selecoption=3">three</a><br />
<a href="Second_page.html?selecoption=4">four</a>
</body>
</html>

这是固定的Second_Page.html文件。所有脚本代码都移动到脚本代码中。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script type="text/javascript">
function getUrlVar(varName) { //returns empty string if variable name not found in URL
  if (!varName) return ''; //no variable name specified. exit and return empty string

  varName = varName.toLowerCase(); //convert to lowercase
  var params = location.search; //get URL

  if (params == '') return ''; //no variables at all. exit and return empty string

  var vars = params.split('?')[1].split('&'); //get list of variable+value strings

  for (var i = 0; i < vars.length; i++) { //check each variable
   var varPair = vars[i].split('='); //split variable and its value

   if (varPair.length > 1) { //has "=" separator

     if (varPair[0].toLowerCase() == varName) { //same variable name?
       return varPair[1]; //found variable. exit and return its value
     } //else: check next variable, if any

   } //else: is not an array. i.e.: invalid URL variable+value format. ignore it
  }
  return ''; //no matching variable found. exit and return empty string
}

function show() {
  var value = getUrlVar('selecoption'); //get variable value
  if (!value) return; //variable not found
  if (parseInt(value) == NaN) return; //value is not a number

  var sel = document.getElementById('form1').selecoption;
  for (var i=0; i < sel.length; i++) {
    if (sel.options[i].value == value) {
      document.getElementById('form1').selecoption.value = value;
      return;
    }
  }
}
</script>
</head>

<body onLoad="show();">
<form id="form1">
  <select name="selecoption">
    <option value="1">ONE</option>
    <option value="2">TWO</option>
    <option value="3">THREE</option>
    <option value="4">FOUR</option>
  </select>
</form>
</body>
</html>