就像标题说的那样,我正在尝试编写解释多个单选按钮(<input type="radio">
)的 JavaScript,变量输出取决于选择的单选按钮,然后根据 html 的请求显示结果<button>
。我一直在研究试图找到适合我正在尝试做的事情的 JavaScript 代码,到目前为止,这就是我发现的: var myFunc = function () { alert('1');
};
myFunc(); // alerts '1'
var myFunc = function () {
alert('2');
};
myFunc(); // alerts '2'
和
<script>
function myFunction()
{
document.getElementById("demo").innerHTML="PASSED!";
}
</script>
</head>
<body>
<p>Click the button to trigger a function.</p>
<button onclick="myFunction()">Click me</button>
<p id="demo"></p>
和
var checked = false, radios = document.getElementsById('radiogroup1');
for (var i = 0, radio; radio = radios[i]; i++) {
if (radio.checked) {
checked = true;
break;
}
}
if (!checked) {
alert("Please select option one");
radios.focus();
return false;
}
return true;
. 然后我开始尝试将它们全部组合成一个功能代码并得到这个:
`<head>
<script type="text/javascript">
var myFunc = function ('1') {
var checked = false, radios = document.getElementById('_pass1');
var checked = false, radios = document.getElementById('_pass2');
var checked = false, radios = document.getElementById('_pass3');
var checked = false, radios = document.getElementById('_pass4');
for (var i = 0, radio; radio = radios[i]; i++) {
if (radio.checked) {
checked = true;
document.getElementById('pass_fail_').innerHTML="PASSED!";
break;
}
}
if (!checked) {
alert("Please select all options");
radios.focus();
return false;
}
return true;
break;
var myFunc = function ('2') {
var checked = false, radios = document.getElementById('_pass1');
var checked = false, radios = document.getElementById('_pass2');
var checked = false, radios = document.getElementById('_pass3');
var checked = false, radios = document.getElementById('_fail4');
for (var i = 0, radio; radio = radios[i]; i++) {
if (radio.checked) {
checked = true;
document.getElementById('pass_fail_').innerHTML="PASSED!";
break;
}
}
if (!checked) {
alert("Please select all options");
radios.focus();
return false;
}
return true;
break;
myFunc('1); // function '1', function '2', function '3', function '4', function '5', function '6', function '7', function '8', function '9', function '10', function '11', function '12', function '13', function '14', function '15', function '16' </script>
`</head>
`<body>
<p><b>Important Text</b></p>
</br>
<form id="_form_3">
<input type="radio" name="grade" id="_pass3" value="pass"><font color="green">Pass</font></br>
<input type="radio" name="grade" id="_fail3" value="fail"><font color="red">Fail</font>
</form>
<p>COmments</p>
<textarea cols="95" rows="15">
</textarea>
</br>
<hr>
<p><b>Step, Slide, Switch</b><p>
</br>
<form id="_form_4">
<input type="radio" name="grade" id="_pass4" value="pass"><font color="green">Pass</font></br>
<input type="radio" name="grade" id="_fail4" value="fail"><font color="red">Fail</font>
</form>
<p>Comments</p>
<textarea cols="95" rows="15">
</textarea>
</br>
<hr>
</form>
<button onclick="myFunc('1')">PRESSS ME!</button>
<p id="pass_fail_"></p>
`</body>
`</html>
现在这不起作用,显然遗漏了一些我保留的部分,以试图限制你的眼睛疲劳(所有遗漏的部分都遵循共享的类似路线),但无论是 JavaScript 部分还是我的编码技能失败,偏向后者,我不知道。就像标题说的那样,我正在尝试编写解释多个单选按钮(<input type="radio">
)的 JavaScript,变量输出取决于选择的单选按钮,然后根据 html 的请求显示结果<button>
,我有第二个问题与第一的。JavaScript 可以运行一个很好的脚本来做我想做的事情,还是我应该使用 flash 或 .net 语言?