我应该对函数参数进行什么更改以链接到另一个页面
what change in function argument should I make to link to another page?
大家好,我是网络编程的新手,所以请帮助我解决这个问题。我正在尝试将我从另一个论坛获得的 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>
相关文章:
- 想要打开从链接到另一个页面的模式弹出窗口
- 将按钮链接到另一个页面的一部分
- JavaScript:将输入范围行为链接到另一个元素
- 正在验证并链接到另一个页面
- 将登录链接到另一个网站而不提供密码
- 在JointJS中将一个链接链接到另一个
- 点击jsp页面上的链接打开一个文件
- 将href链接到另一个HTML文档中的锚点并将其居中
- 我想让HTML链接打开一个js手风琴
- 使用 JavaScript 向一组链接中的第一个链接添加一个属性
- 链接到另一个带有jQuery元素的页面
- 为链接打开一个新窗口,但希望下一页仍在“主体”框架中
- 将 href 值从 标记链接到另一个 html 页面
- 链接到另一个具有过渡的页面
- 使用JavaScript和html将图像和超链接从一个位置随机移动到另一个位置
- 使用一个页面中的链接链接到另一个页面的不同脚本
- 如何在jquery中链接另一个HTML页面到海报图像
- 如何使用按钮链接另一个页面
- 转换链接到另一个链接- javascript
- 我如何使一个图像的链接,链接到另一个图像,使用javascript