好吧,目标是使无线电按钮true或false触发触发事件,导致与消息相对应的文本。我失败了。如果您宁愿解决问题,也不愿解释我做错了什么,那也可以。我知道你们都很忙。我正在努力学习,如果您愿意解释我做错了什么,所以我可以更好地理解JavaScript语法和逻辑,我将非常感谢它!https://codepen.io/slimmwillis/pen/wmrmjp
var x = 3;
var y = 2;
var z = x + y;
document.getElementById("num").innerHTML = z;
function myFunction() {
var OnSubmit;
var clicked = new Boolean().getvalue();
/* I think the error is I am failing to get a value for clicked from Boolean which I believe is declared as either false or true in the form above. Help me Sensei!*/
if (clicked == true) {
OnSubmit = "That is correct, you know Maths";
} else if (clicked == false) {
OnSubmit = "Incorrect! Dummy!";
} else {
OnSubmit = "Please Select 1";
}
document.getElementById("clicker").innerHTML = OnSubmit;
}
<!DOCTYPE html>
<html>
<body>
<h1>So easy I could cry</h1>
<p>3+2=</p>
<!-- displays the result of x+y-->
<p id="num"></p>
<!-- True of False form-->
<form>
<input type="radio" name="Boolean" value="true" checked> True<br>
<input type="radio" name="Boolean" value="false"> False<br>
<input type="submit" value="Submit">
<!-- The if.. elseif...else statement-->
<h2> So hard I could Cry</h2>
<p id="clicker"></p>
</form>
</body>
</html>
- 您需要将
submit
事件绑定到您的form
以调用您的功能myFunction
。
document.queryselector('form'(。addeventListener('submit',myfunction(;
- 要获得
clicked
的布尔值,请执行以下选择器[name="Boolean"]:checked
以获取已检查的ReadioButton:
document.queryselectorall('[name =" boolean"]:checked'(。foreach(function(e({ clicked = e.getAttribute('value'(==='true';}(;
var x = 3;
var y = 2;
var z = x + y;
document.getElementById("num").innerHTML = z;
var clicker = document.getElementById("clicker")
function myFunction(e) {
e.preventDefault(); // remove this if you need to submit that form.
var onSubmit,
clicked;
document.querySelectorAll('[name="Boolean"]:checked').forEach(function(e) {
clicked = e.getAttribute('value') === 'true';
});
/* I think the error is I am failing to get a value for clicked from Boolean which I believe is declared as either false or true in the form above. Help me Sensei!*/
if (clicked) {
onSubmit = "That is correct, you know Maths";
} else if (!clicked) {
onSubmit = "Incorrect! Dummy!";
} else {
onSubmit = "Please Select 1";
}
clicker.innerHTML = onSubmit;
}
document.querySelector('form').addEventListener('submit', myFunction);
<!DOCTYPE html>
<html>
<body>
<h1>So easy I could cry</h1>
<p>3+2=</p>
<!-- displays the result of x+y-->
<p id="num"></p>
<!-- True of False form-->
<form>
<input type="radio" name="Boolean" value="true" checked> True<br>
<input type="radio" name="Boolean" value="false"> False<br>
<input type="submit" value="Submit">
<!-- The if.. elseif...else statement-->
<h2> So hard I could Cry</h2>
<p id="clicker"></p>
</form>
</body>
</html>
您可以使用jQuery API很简单:
<form class='myform'>
<input type='radio' id='myRadioBox'>
</form>
$(document).ready(function(){
$('.myform').submit(function(event){
event.preventDefault();
var result = document.getElementById('myRadioBox').value;
if(result == 'checked'){
// display true/false message
}else{
// display false/true message
}
})
});
'event.preventdefault(('是为了防止表格提交!如果您在提交表格之前检查条件,则应始终包括在内,此后您可以提交表格!
$('.myform').submit();
使无线电按钮具有单一类迭代课程,并检查您的状况!即
for(var i=0; i<radioClass.length; i++){
if(result == checked){
// do something
}else{
// do something else!
}
}
var result = document.getElementByClassName('radioClass');
// result will be an array of radio buttons;