Javascript 更改Submit onclick location.href的多个单选按钮…可能存在语法错误

Javascript 更改Submit onclick location.href的多个单选按钮…可能存在语法错误,javascript,forms,onclick,radio-button,form-submit,Javascript,Forms,Onclick,Radio Button,Form Submit,我正在对单选按钮使用javascript来更改submit onclick location.href,具体取决于选择的单选按钮。但是我认为我的语法可能有问题,因为按钮不能正常工作(我认为它不能正确地从单选按钮中提取值)。提前感谢 代码如下: <head> <script type="text/javascript"> <!-- // return the value of the radio button that is checked // return an

我正在对单选按钮使用javascript来更改submit onclick location.href,具体取决于选择的单选按钮。但是我认为我的语法可能有问题,因为按钮不能正常工作(我认为它不能正确地从单选按钮中提取值)。提前感谢

代码如下:

<head>
<script type="text/javascript">
<!--
// return the value of the radio button that is checked
// return an empty string if none are checked, or
// there are no radio buttons
function getCheckedValue(radioObj) {
if(!radioObj)
    return "";
var radioLength = radioObj.length;
if(radioLength == undefined)
    if(radioObj.checked)
        return radioObj.value;
    else
        return "";
for(var i = 0; i < radioLength; i++) {
    if(radioObj[i].checked) {
        return radioObj[i].value;
    }
}
return "";
}

// set the radio button with the given value as being checked
// do nothing if there are no radio buttons
// if the given value does not exist, all the radio buttons
// are reset to unchecked
function setCheckedValue(radioObj, newValue) {
if(!radioObj)
    return;
var radioLength = radioObj.length;
if(radioLength == undefined) {
    radioObj.checked = (radioObj.value == newValue.toString());
    return;
}
for(var i = 0; i < radioLength; i++) {
    radioObj[i].checked = false;
    if(radioObj[i].value == newValue.toString()) {
        radioObj[i].checked = true;
    }
}
}
//-->
</script>
</head>

<body>

<form name="radioExampleForm" method="get" action="" onsubmit="return false;">
<p><label for="number0"><input type="radio" value="http://www.google.com" name="number"     id="number0"> Zero</label>
&nbsp;<label for="number1"><input type="radio" value="http://www.ebay.com" name="number" id="number1"> One</label>
&nbsp;<label for="number2"><input type="radio" value="http://www.gamestop.com" name="number" id="number2"> Two</label>
&nbsp;<label for="number3"><input type="radio" value="http://www.amazon.com" name="number" id="number3"> Three</label>
&nbsp;<label for="number4"><input type="radio" value="http://www.usatoday.com" name="number" id="number4"> Four</label>
<p>
<input type="button" onclick="location.href='+getCheckedValue(document.forms['radioExampleForm'].elements['number']);" value="Show Checked Value">

ORIGNAL SUBMIT CODE THAT MADE AN ALERT BOX RATHER THAN location.href = <input type="button" onclick="alert('Checked value is: '+getCheckedValue(document.forms['radioExampleForm'].elements['number']));" value="Show Checked Value">

</form>
</body>

零
一
二
三
四

生成警报框而不是位置的原始提交代码。href=

这只是一个语法错误

将其更改为:

onclick="window.location.href = (getCheckedValue(document.forms['radioExampleForm'].elements['number']));"

太好了,我知道这是件小事。非常感谢。