我应该对函数参数进行什么更改以链接到另一个页面

what change in function argument should I make to link to another page?

本文关键字:链接 另一个 函数 参数 什么 我应该      更新时间:2023-09-26

大家好,我是网络编程的新手,所以请帮助我解决这个问题。我正在尝试将我从另一个论坛获得的 2 个下拉列表链接在一起,然后我正在尝试根据所选选项将 go 按钮链接到另一个网页。为了转到另一个页面,我得到了该功能,但是当我尝试链接时它不起作用

这是代码。有两个功能 1.用于链接下拉列表 2.用于转到另一个页面。但它不起作用,因为我已经编写了 onClick 函数转到新页面。

<html>
<head>
<script type="text/javascript">
function setOptions(chosen) {
var selbox = document.myform.opttwo;
selbox.options.length = 0;
if (chosen == " ") {
selbox.options[selbox.options.length] = new Option('Please select one of the options        above first',' ');
}
if (chosen == "1") {
selbox.options[selbox.options.length] = new
Option('http://www.google.com','oneone');
selbox.options[selbox.options.length] = new
Option('first choice - option two','onetwo');
}
if (chosen == "2") {
selbox.options[selbox.options.length] = new
Option('second choice - option one','twoone');
selbox.options[selbox.options.length] = new
Option('second choice - option two','twotwo');
}
if (chosen == "3") {
selbox.options[selbox.options.length] = new
Option('third choice - option one','threeone');
selbox.options[selbox.options.length] = new
Option('third choice - option two','threetwo');
}
}
</script>
<SCRIPT LANGUAGE="JavaScript">
<!--
function JumpToIt(frm) {
var newPage = frm.url.options[frm.url.selectedIndex].value
if (newPage != "None") {
    location.href=newPage
}
}
//-->
</SCRIPT>
</head>
<form name="myform"><div align="center">
<select name="optone" size="1"
onchange="setOptions(document.myform.optone.options
[document.myform.optone.selectedIndex].value);">
<option value=" " selected="selected"> </option>
<option value="1">First Choice</option>
<option value="2">Second Choice</option>
<option value="3">Third Choice</option>
</select><br> <br>
<select name="opttwo" size="1">  
<option value=" " selected="selected">Please select one of the options above     first</option>
</select>
<INPUT TYPE=BUTTON VALUE="Go!" onClick="JumpToIt(this.myform)">
</div></form>
</html>

所以有人请告诉我我应该在 onclick 函数中写什么,以便它进入链接页面。

第二个下拉列表是否正确填充?我的印象是,要创建一个新选项,您使用了document.createElement("选项"(,如下所示:

var opt = document.createElement("option");
opt.text = "text";
opt.value = "value";
selbox.options[selbox.options.length] = opt;

但是,我认为主要问题是表单中没有带有 id 或名称"url"的元素。 我相信你需要改变

var newPage = frm.url.options[frm.url.selectedIndex].value

var newPage = frm.opttwo.options[frm.opttwo.selectedIndex].value

<INPUT TYPE=BUTTON VALUE="Go!" onClick="JumpToIt(this.myform)">这里的"this"指的是输入标签,它没有"myform"的属性。

你可能的意思是:

<INPUT TYPE=BUTTON VALUE="Go!" onClick="JumpToIt('myform')">

但在这个函数中:

function JumpToIt(frm) {
var newPage = frm.url.options[frm.url.selectedIndex].value
if (newPage != "None") {
    location.href=newPage
}
}

你给它传递了一个表单名称,但你的表单中没有一个名为"url"的元素,你有"opttwo",所以将"opttwo"换成"url",这可能会起作用。

<html>
<head>
<script type="text/javascript"> 
    function setOptions(chosen) 
    { 
        var selbox = document.myform.opttwo;  
        selbox.options.length = 0; 
        if (chosen == " ") 
        { 
            selbox.options[selbox.options.length] = new Option('Please select one of the options        above first',' ');  } 
            if (chosen == "1") { selbox.options[selbox.options.length] = new Option('http://www.google.com','oneone'); selbox.options[selbox.options.length] = new Option('first choice - option two','onetwo'); } 
            if (chosen == "2") { selbox.options[selbox.options.length] = new Option('second choice - option one','twoone'); selbox.options[selbox.options.length] = new Option('second choice - option two','twotwo'); } 
            if (chosen == "3") { selbox.options[selbox.options.length] = new Option('third choice - option one','threeone'); selbox.options[selbox.options.length] = new Option('third choice - option two','threetwo'); 
        } 
    } 
    function JumpToIt(frm) 
    { 
        var newPage = frm.opttwo.options[frm.opttwo.options.selectedIndex].value;

        if (newPage != "None") { location.href=newPage; } 
    } 
</script>
</head>
<form name="myform">
    <div align="center">
        <select name="optone" size="1" onchange="setOptions(document.myform.optone.options [document.myform.optone.selectedIndex].value);">
            <option value=" " selected="selected"></option>
            <option value="1">First Choice</option>
            <option value="2">Second Choice</option>
            <option value="3">Third Choice</option>
        </select>
        <br>
        <br>
        <select name="opttwo" size="1">
            <option value=" " selected="selected">Please select one of the options above first</option>
        </select>
        <input type="BUTTON" value="Go!" onclick="JumpToIt(document.myform)">
    </div>
</form>
</html>