2012-08-23 53 views
0

这里我有两个HTML页面,First_page.html和Second_page.html,请帮助寻找出了我正确的Javascript代码...请

在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 URL参数选择两个不同的下拉菜单中的即“selecoption” &“selecsecondoption”。请帮助....

+0

你必须比这更具体。什么是正确的代码?你有什么尝试? – Blender

回答

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