1

我正在使用http://www.somacon.com/p143.php javascript 作为单选按钮来更改提交 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>
4

1 回答 1

0

这只是一个语法错误。

将其更改为:

onclick="window.location.href = (getCheckedValue(document.forms['radioExampleForm'].elements['number']));"
于 2012-04-10T17:23:55.323 回答