我有一个快速打字游戏。我也有一个带有3个单选按钮的表单,我想根据所选的单选按钮选择游戏的级别。我该怎么办?
我在这里尝试使用document.querySelector('input[name="genderS"]:checked').value;
此代码。但这没用
const levels = {
easy: 5,
medium: 3,
hard: 1
};
let easyChecked = document.querySelector('#radio-easy').checked;
let mediumChecked = document.querySelector('#radio-medium').checked;
let hardChecked = document.querySelector('#radio-hard').checked;
var currentLevel = levels.medium;
let time = currentLevel;
let score = 0;
let isPlaying;
// To change level
function isChecked() {
}
我希望currentLevel变量会根据单选按钮进行更改。
答案 0 :(得分:0)
类似这样的东西:
var currentLevel = null; // not set yet
let easyChecked = document.querySelector('#radio-easy');
let mediumChecked = document.querySelector('#radio-medium');
let hardChecked = document.querySelector('#radio-hard');
easyChecked.addEventListener("change", function() {
currentLevel = this.checked; // update currentLevel on change
});
mediumChecked.addEventListener("change", function() {
currentLevel = this.checked; // update currentLevel on change
});
hardChecked.addEventListener("change", function() {
hardChecked = this.checked; // update currentLevel on change
});
现在 currentLevel 将在单选按钮更改时更新。
答案 1 :(得分:0)
简单地,使您的单选按钮HTML代码具有以下相同的名称:
ReSharper | Options | Code Inspection | Settings | Read settings from editorconfig and project settings
然后通过foreach循环将所有单选按钮与<input type="radio" name="level" value="easy"> Easy<br>
<input type="radio" name="level" value="medium"> Medium<br>
<input type="radio" name="level" value="hard"> Hard<br>
事件绑定在一起
change