关于单选按钮 [英] Regarding radio buttons
本文介绍了关于单选按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下带有单选按钮的代码,它在选择后会发出带有单选按钮值的警报,但是可以将google.com这样的网站链接到选择.
Hi,
i have the following code with radio buttons it is giving an alert with radio button value after the selection but is it possible to link a website like google.com to the selection.
<script language="javascript">
<!--
function RadioCheck() {
var selection = document.quiz.radio;
for (i=0; i<selection.length; i++)
if (selection[i].checked == true)
alert(selection[i].value)
}
//-->
</script>
<form name="quiz">
<input type="radio" name="radio" value="red">red<br>
<input type="radio" name="radio" value="orange">orange<br>
<input type="radio" name="radio" value="yellow">yellow<br>
<input type="submit" value="Check Answer" onClick="RadioCheck()">
</form>
预先感谢您的帮助.
Thanks in advance for your help.
推荐答案
使用Window.Location
或Window.Open
<script language="javascript">
<!--
function RadioCheck() {
var selection = document.quiz.radio;
for (i=0; i<selection.length; i++)
if (selection[i].checked == true)
window.location = "http://www.google.com/";
}
//-->
</script>
由于您使用的是提交"按钮,因此需要返回false
以防止表单提交:
Because you''re using a submit button, you need to returnfalse
to prevent the form submitting:
<script language="javascript">
<!--
function RadioCheck() {
var selection = document.quiz.radio, urls = [
'http://www.google.com/', 'http://www.bing.com/', 'http://yahoo.com/'
];
for (i=0; i<selection.length; i++)
if (selection[i].checked)
window.location = urls[i];
return false; // ADD THIS!
}
//-->
</script>
您还需要在onclick中添加return
:
You need a return
in the onclick too:
<input type="submit" value="Check Answer" onClick="return RadioCheck()">
这篇关于关于单选按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文