如何找到适合我的正确 JavaScript 代码

问题描述 投票:0回答:2

这里我有两个 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>

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

javascript redirect drop-down-menu onload url-parameters
2个回答
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>

0
投票

试试这个:

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

在 Second_Page.html 的末尾

© www.soinside.com 2019 - 2024. All rights reserved.