如何在 JavaScript 中使用选中的单选按钮值
How to use checked radio button values in JavaScript
您好,我正在尝试创建一个血液酒精含量计算器。我有一个性别乘数,需要根据其无线电输入更改其值。因此,如果用户单击男性,则性别乘数需要为 0.55,如果用户单击女性,则性别乘数为 0.68
<div class="viewport">
<div class="gender-buttons" id="gender-buttons">
<input class="gender-button" type="radio" name="tools" id="tool-1" value="0.55">
<label class="for-gender-button" for="tool-1">Male</label>
<input class="gender-button" type="radio" name="tools" id="tool-2" value="0.68">
<label class="for-gender-button" for="tool-2">Female</label>
</div>
<div class="weight-input" >
<h3>ENTER WEIGHT</h3>
<input id="weight-input" type="number" placeholder="75kg" required>
</div>
<div class="alcohol-content" >
<h3>AMOUNT OF DRINKS</h3>
<input id="alcohol-content" type="number" placeholder="5" required>
</div>
<div class="alcohol-type">
<h3>TYPE OF DRINKS</h3>
<select name="type-of-alcohol" id="alcohol-type">
<option value="1">Shots of Spirits</option>
<option value="1">Glasses of Wine</option>
<option value="1">Pints of Beer</option>
</select>
</div>
<div class="elapsed-time">
<h3>ELAPSED TIME</h3>
<input id = "elapsed-time" type="number" placeholder="2 hours ago" required>
</div>
<div class="submit-button" >
<input type="submit" id="submit-button">
</div>
<div class="result-container" id="result-container">
<h5>YOUR BAC IS</h5>
</div>
</div>
let weightElement = document.getElementById("weight-input");
let amountElement = document.getElementById("alcohol-content");
let submitButton = document.getElementById("submit-button");
const alcoholTypeElement = document.getElementById("alcohol-type");
const elapsedTimeElement = document.getElementById("elapsed-time");
这是单选按钮的变量
const genderButtonElement = document.querySelector('input[name="tools"]:checked');
//Function to calculate BAC
submitButton.addEventListener('click', function(){
const weight = parseInt(weightElement.value);
const amount = parseInt(amountElement.value);
const alcoholType = parseFloat(alcoholTypeElement.value);
const gramsOfAlcohol = (alcoholType*amount)*14;
函数内的新变量采用 genderButtonElement 的值,parseFloat 的值将在下面的 bloodAlcoholContent 变量中使用
const genderButton = parseFloat(genderButtonElement.value);
const bloodAlcoholContent = (gramsOfAlcohol / ((weight * 1000) * genderButton))*100;
const elapsedTime = parseInt(elapsedTimeElement.value) * 0.015;
const finalBac = bloodAlcoholContent - elapsedTime;
document.getElementById("result-container").innerHTML =
finalBac.toFixed(2) + " " + "BAC";
})
chrome 控制台上的问题显示“未捕获的类型错误,无法读取 'null' 的 属性”并且没有使用 genderButton 的答案(来自收音机的输入) bloodAlcoholContent
的计算
谢谢
请 (1) 添加 jquery 和 (2) 添加验证码。请尝试以下代码:
(我还更改了与“饮料类型”相关的值,作为演示)。
<script src="https://code.jquery.com/jquery-3.5.1.js" integrity="sha256-QWo7LDvxbWT2tbbQ97B53yJnYU3WhH/C8ycbRAkjPDc=" crossorigin="anonymous"></script>
<div class="viewport">
<div class="gender-buttons" id="gender-buttons">
<input class="gender-button" type="radio" name="tools" id="tool-1" value="0.55">
<label class="for-gender-button" for="tool-1">Male</label>
<input class="gender-button" type="radio" name="tools" id="tool-2" value="0.68">
<label class="for-gender-button" for="tool-2">Female</label>
</div>
<div class="weight-inputx" >
<h3>ENTER WEIGHT</h3>
<input id="weight-input" type="number" placeholder="75kg" required>
</div>
<div class="alcohol-contentx" >
<h3>AMOUNT OF DRINKS</h3>
<input id="alcohol-content" type="number" placeholder="5" required>
</div>
<div class="alcohol-type">
<h3>TYPE OF DRINKS</h3>
<select name="type-of-alcohol" id="alcohol-type">
<option value="1">Shots of Spirits</option>
<option value="2">Glasses of Wine</option>
<option value="3">Pints of Beer</option>
</select>
</div>
<div class="elapsed-time">
<h3>ELAPSED TIME</h3>
<input id = "elapsed-time" type="number" placeholder="2 hours ago" required>
</div>
<div class="submit-button" >
<input type="submit" id="submit-button" onclick="javascript:trigger1();">
</div>
<div class="result-container" id="result-container">
<h5>YOUR BAC IS</h5>
</div>
</div>
<script>
//Function to calculate BAC
function trigger1()
{
let weightElement = document.getElementById("weight-input");
let amountElement = document.getElementById("alcohol-content");
let submitButton = document.getElementById("submit-button").value;
const alcoholTypeElement = document.getElementById("alcohol-type");
const elapsedTimeElement = document.getElementById("elapsed-time");
const genderButtonElement = document.querySelector('input[name="tools"]:checked');
if (genderButtonElement == null){
alert("Please choose the Gender"); return false;
}
if (weightElement.value == ""){
alert("Please enter the weight"); return false;
}
if (amountElement.value == ""){
alert("Please enter the Amount of Drinks"); return false;
}
if (elapsedTimeElement.value == ""){
alert("Please enter the Elapsed time"); return false;
}
const weight = parseInt(weightElement.value);
const amount = parseInt(amountElement.value);
const alcoholType = parseFloat(alcoholTypeElement.value);
const gramsOfAlcohol = (alcoholType*amount)*14;
const genderButton = parseFloat(genderButtonElement.value);
const bloodAlcoholContent = (gramsOfAlcohol / ((weight * 1000) * genderButton))*100;
const elapsedTime = parseInt(elapsedTimeElement.value) * 0.015;
const finalBac = bloodAlcoholContent - elapsedTime;
document.getElementById("result-container").innerHTML = finalBac.toFixed(2) + " " + "BAC";
}
</script>
您好,我正在尝试创建一个血液酒精含量计算器。我有一个性别乘数,需要根据其无线电输入更改其值。因此,如果用户单击男性,则性别乘数需要为 0.55,如果用户单击女性,则性别乘数为 0.68
<div class="viewport">
<div class="gender-buttons" id="gender-buttons">
<input class="gender-button" type="radio" name="tools" id="tool-1" value="0.55">
<label class="for-gender-button" for="tool-1">Male</label>
<input class="gender-button" type="radio" name="tools" id="tool-2" value="0.68">
<label class="for-gender-button" for="tool-2">Female</label>
</div>
<div class="weight-input" >
<h3>ENTER WEIGHT</h3>
<input id="weight-input" type="number" placeholder="75kg" required>
</div>
<div class="alcohol-content" >
<h3>AMOUNT OF DRINKS</h3>
<input id="alcohol-content" type="number" placeholder="5" required>
</div>
<div class="alcohol-type">
<h3>TYPE OF DRINKS</h3>
<select name="type-of-alcohol" id="alcohol-type">
<option value="1">Shots of Spirits</option>
<option value="1">Glasses of Wine</option>
<option value="1">Pints of Beer</option>
</select>
</div>
<div class="elapsed-time">
<h3>ELAPSED TIME</h3>
<input id = "elapsed-time" type="number" placeholder="2 hours ago" required>
</div>
<div class="submit-button" >
<input type="submit" id="submit-button">
</div>
<div class="result-container" id="result-container">
<h5>YOUR BAC IS</h5>
</div>
</div>
let weightElement = document.getElementById("weight-input");
let amountElement = document.getElementById("alcohol-content");
let submitButton = document.getElementById("submit-button");
const alcoholTypeElement = document.getElementById("alcohol-type");
const elapsedTimeElement = document.getElementById("elapsed-time");
这是单选按钮的变量
const genderButtonElement = document.querySelector('input[name="tools"]:checked');
//Function to calculate BAC
submitButton.addEventListener('click', function(){
const weight = parseInt(weightElement.value);
const amount = parseInt(amountElement.value);
const alcoholType = parseFloat(alcoholTypeElement.value);
const gramsOfAlcohol = (alcoholType*amount)*14;
函数内的新变量采用 genderButtonElement 的值,parseFloat 的值将在下面的 bloodAlcoholContent 变量中使用
const genderButton = parseFloat(genderButtonElement.value);
const bloodAlcoholContent = (gramsOfAlcohol / ((weight * 1000) * genderButton))*100;
const elapsedTime = parseInt(elapsedTimeElement.value) * 0.015;
const finalBac = bloodAlcoholContent - elapsedTime;
document.getElementById("result-container").innerHTML =
finalBac.toFixed(2) + " " + "BAC";
})
chrome 控制台上的问题显示“未捕获的类型错误,无法读取 'null' 的 属性”并且没有使用 genderButton 的答案(来自收音机的输入) bloodAlcoholContent
的计算谢谢
请 (1) 添加 jquery 和 (2) 添加验证码。请尝试以下代码:
(我还更改了与“饮料类型”相关的值,作为演示)。
<script src="https://code.jquery.com/jquery-3.5.1.js" integrity="sha256-QWo7LDvxbWT2tbbQ97B53yJnYU3WhH/C8ycbRAkjPDc=" crossorigin="anonymous"></script>
<div class="viewport">
<div class="gender-buttons" id="gender-buttons">
<input class="gender-button" type="radio" name="tools" id="tool-1" value="0.55">
<label class="for-gender-button" for="tool-1">Male</label>
<input class="gender-button" type="radio" name="tools" id="tool-2" value="0.68">
<label class="for-gender-button" for="tool-2">Female</label>
</div>
<div class="weight-inputx" >
<h3>ENTER WEIGHT</h3>
<input id="weight-input" type="number" placeholder="75kg" required>
</div>
<div class="alcohol-contentx" >
<h3>AMOUNT OF DRINKS</h3>
<input id="alcohol-content" type="number" placeholder="5" required>
</div>
<div class="alcohol-type">
<h3>TYPE OF DRINKS</h3>
<select name="type-of-alcohol" id="alcohol-type">
<option value="1">Shots of Spirits</option>
<option value="2">Glasses of Wine</option>
<option value="3">Pints of Beer</option>
</select>
</div>
<div class="elapsed-time">
<h3>ELAPSED TIME</h3>
<input id = "elapsed-time" type="number" placeholder="2 hours ago" required>
</div>
<div class="submit-button" >
<input type="submit" id="submit-button" onclick="javascript:trigger1();">
</div>
<div class="result-container" id="result-container">
<h5>YOUR BAC IS</h5>
</div>
</div>
<script>
//Function to calculate BAC
function trigger1()
{
let weightElement = document.getElementById("weight-input");
let amountElement = document.getElementById("alcohol-content");
let submitButton = document.getElementById("submit-button").value;
const alcoholTypeElement = document.getElementById("alcohol-type");
const elapsedTimeElement = document.getElementById("elapsed-time");
const genderButtonElement = document.querySelector('input[name="tools"]:checked');
if (genderButtonElement == null){
alert("Please choose the Gender"); return false;
}
if (weightElement.value == ""){
alert("Please enter the weight"); return false;
}
if (amountElement.value == ""){
alert("Please enter the Amount of Drinks"); return false;
}
if (elapsedTimeElement.value == ""){
alert("Please enter the Elapsed time"); return false;
}
const weight = parseInt(weightElement.value);
const amount = parseInt(amountElement.value);
const alcoholType = parseFloat(alcoholTypeElement.value);
const gramsOfAlcohol = (alcoholType*amount)*14;
const genderButton = parseFloat(genderButtonElement.value);
const bloodAlcoholContent = (gramsOfAlcohol / ((weight * 1000) * genderButton))*100;
const elapsedTime = parseInt(elapsedTimeElement.value) * 0.015;
const finalBac = bloodAlcoholContent - elapsedTime;
document.getElementById("result-container").innerHTML = finalBac.toFixed(2) + " " + "BAC";
}
</script>