尝试使用javascript重定向
我对javascript非常陌生,我正试图根据输入的类型使不同的事件发生。我的html标题中包含以下内容:尝试使用javascript重定向,javascript,html,redirect,Javascript,Html,Redirect,我对javascript非常陌生,我正试图根据输入的类型使不同的事件发生。我的html标题中包含以下内容: <script type="text/javascript"> function validateForm(){ var val=document.getElementsByName("destination"); if (val == "deprecated"){ window.location="http://website.com/";
<script type="text/javascript">
function validateForm(){
var val=document.getElementsByName("destination");
if (val == "deprecated"){
window.location="http://website.com/";
}
}
</script>
但这也不起作用
有人知道问题是什么吗?
谢谢。您需要从所选选项中获取值。像这样:
var index = document.getElementsByName("destination").selectedIndex;
var val=document.getElementsByName("destination").options[index].value;
这将检索所选选项的值。您需要从所选元素中获取值。由于document.getElementsByName返回数组,请尝试使用
var val = document.getElementsByName("destination")[0].value
您缺少href属性,您要使用:
window.location.href = 'URL';
执行以下操作
<select name="destination" id="destination">
将
警报(val)
放入您的if
中,查看是否有任何计算结果为true我修复了您的功能并对其进行了测试:
function validateForm(){
var val=document.getElementsByName("destination");
var theSelectedOption = val[0].options[val[0].selectedIndex].value;
if (theSelectedOption == "deprecated"){
window.location="http://website.com/";
}
}
尝试使用id而不是名称,id是唯一的元素。Javascript支持
var destination = document.getElementById("destination");
if (destination.options[destination.selectedIndex].value == "deprecated"){
window.location="http://website.com/";
}
HTML
<select id="destination">
<option value="current_builds">Current Builds</option>
<option value="deprecated">Deprecated Files</option>
<option value="mailing_list">Mailing List</option>
</select>
<br />
<input type="button" value="next >" onclick="javascript:validateForm();" />
当前版本
不推荐使用的文件
邮件列表
尝试一下jquery
哇,真管用。但我不明白为什么。为什么它会作为数组提交?该数组中有多个元素吗?没有,只有一个元素,即0元素。是的,就像在arrayWell中一样,想一想。您正在调用getElementsByName。这是元素,就像在多重粒子中一样。Javascript将返回具有给定名称的所有元素,因此,它将以数组的形式返回它们。哪怕只是一个元素,你能给我一点解释,让我知道未来的情况吗?比如,您如何知道选择val[0]而不是val[1]或其他什么?是否会有选择val[1]的时间,您能给我一个这样的例子吗?另外,为什么我需要.selectedIndex部分呢?
getElementsByName(“destination”)
方法访问所有名为“destination”的元素,它返回找到的所有元素的数组。这里val[0]是找到的第一个元素,val[1]是在html中找到的第二个元素(如果有)。。。selectedIndex设置或返回下拉列表中所选选项的索引。请参阅名称getElementsByName
,它的元素是复数,意思是可能有多个元素具有该名称,因此返回数组形式的集合。另一方面,getElementById
是单数的,一个元素应该返回到dah,这很有意义。谢谢。实际上,还有一个简单的问题:我们是否可以这样做:var theSelectedOption=val[0]。值,因为当您按下按钮时,它不会自动返回所选索引的值吗?
val=document.getElementsById("destination").value;
function validateForm(){
var val=document.getElementsByName("destination");
var theSelectedOption = val[0].options[val[0].selectedIndex].value;
if (theSelectedOption == "deprecated"){
window.location="http://website.com/";
}
}
var destination = document.getElementById("destination");
if (destination.options[destination.selectedIndex].value == "deprecated"){
window.location="http://website.com/";
}
<select id="destination">
<option value="current_builds">Current Builds</option>
<option value="deprecated">Deprecated Files</option>
<option value="mailing_list">Mailing List</option>
</select>
<br />
<input type="button" value="next >" onclick="javascript:validateForm();" />
<form name="my_form">
<select id="destination">
<option value="current_builds">Current Builds</option>
<option value="deprecated">Deprecated Files</option>
<option value="mailing_list">Mailing List</option>
</select><br/>
<input type="button" value="next >" id="submit">
</form>
$('#submit').click(
function validateForm(){
var e = document.getElementById("destination");
var val = e.options[e.selectedIndex].value;
if (val == "deprecated"){
window.location="http://website.com/";
}
});