gpt4 book ai didi

javascript - 使用 javascripting DOM 脚本隐藏 div,具体取决于单选按钮的总和

转载 作者:行者123 更新时间:2023-12-02 15:52:59 25 4
gpt4 key购买 nike

下午好,我想(如标题所示)根据单选按钮值的总和隐藏我的 div 标签。我无法仅使用查询 DOM 脚本代码很长,希望你能帮助我。非常感谢

<body>

<script>
function checkRadio() {
var selectedAge="";
var selectedBmi="";
var selectedDiabete="";
var description="";
var len = document.row.length;
var i;

function init(){
for (i = 0; i<len; i++) {
if (document.row[i].value);
break;
}

if (selectedAge == "") {
document.getElementByid("radio_error").innnerHTML = "no option selected";
return false
}
else {
document.getElementById("radio_error").innerHTML = "";
return true;
}

}
init();

}

</script>


<script>
function addNumbers()
{
var val1 = parseInt(document.querySelector('input[name = "selectedAge"]:checked').value);
var val2 = parseInt(document.querySelector('input[name = "selectedBmi"]:checked').value);
var val3 = parseInt(document.querySelector('input[name = "selectedDiabete"]:checked').value);
var val4 = parseInt(document.querySelector('input[name = "description"]:checked').value);
var ansD = document.getElementById("answer");

ansD.value = val1 + val2 + val3 + val4;
}

</script>

<script>
var myDiv=document.getElementsByTagName('div');
var allDiv=myDiv.length;
for(var i=0;i<allDiv.i++){
currentDiv=myDiv[i];

if (ansD.value<=15){
div[0].style.display = 'block';
div[1].style.display = 'none';
div[2].style.display = 'none'
} else if (ansD.value<=25){
div[0].style.display = 'none';
div[1].style.display = 'block';
div[2].style.display = 'none';
} else {ansD.value>25){
div[0].style.display = 'none';
div[1].style.display = 'none';
div[2].style.display = 'block';
}

</script>


<table>

<tr>
<th scope="col"></th>
<th scope="col">noRisk</th>
<th scope="col">lowRisk</th>
<th scope="col">mediumRisk</th>
<th scope="col">HighRisk</th>
</tr>
<tr>
<th scope="row"><div class="lefttext">How old are you?</div></th>

<td><input type="radio" id="value1" name="selectedAge" onclick="addNumber(val1)" value="0"checked>1-25</td>
<td><input type="radio" id="value1" name="selectedAge" onclick="addNumber(val1)" value="5">26-40</td>
<td><input type="radio" id="value1" name="selectedAge" onclick="addNumber(val1)" value="8">41-60</td>
<td><input type="radio" id="value1" name="selectedAge" onclick="addNumber(val1)" value="10">1-25</td>
</tr>

<tr>
<th scope="row"><div class="lefttext">What is you BMI?</div></th>
<td><input type="radio" id="value2" name="selectedBmi" onclick="addNumber(val2)" value="0" checked>0-25</td>
<td><input type="radio" id="value2" name="selectedBmi" onclick="addNumber(val2)" value="0">26-30</td>
<td><input type="radio" id="value2" name="selectedBmi" onclick="addNumber(val2)" value="9">31-35</td>
<td><input type="radio" id="value2" name="selectedBmi" onclick="addNumber(val2)" value="10">35+</td>
</tr>
<tr>
<th scope="row"><div class="lefttext">Does anybody in your family have diabetes?</div></th>
<td><input type="radio" id="value3" name="selectedDiabete" onclick="addNumber(val3)" value="0" checked>No</td>
<td><input type="radio" id="value3" name="selectedDiabete" onclick="addNumber(val3)" value="7">Grandparent</td>
<td><input type="radio" id="value3" name="selectedDiabete" onclick="addNumber(val3)" value="15">Sibling</td>
<td><input type="radio" id="value3" name="selectedDiabete" onclick="addNumber(val3)" value="15">Parent</td>
</tr>
<tr>
<th scope="row"><div class="lefttext">How would you describe your diabete</div></th>
<td><input type="radio" id="value4" name="description" onclick="addNumber(val4)" value="0" checked >Low sugar</td>
<td><input type="radio" id="value4" name="description" onclick="addNumber(val4)" value="0">Normal sugar</td>
<td><input type="radio" id="value4" name="description" onclick="addNumber(val4)" value="7">Quite high sugar</td>
<td><input type="radio" id="value4" name="description" onclick="addNumber(val4)" value="10">High sugar</td>
</tr>
</table>
<input type="button" name="submit" value="Submit" onclick="javascript:addNumbers()"/>
Total = <input type="text" id="answer" name="answer" value=""/>



<section>
<h2>Your results:</h2>
<div id="lowResult">
<p> Your results show that you currently have a low risk of
developing diabetes. However, it is important that you
maintain a healthy lifestyle in terms of diet and exercise.
</p>
</div>
<div id="mediumResult">
<p>Your results show that you currently have a medium risk of
developing diabetes. For more information on your risk
factors, and what to do about them, please visit our diabetes
advice website at <a href="http://www.zha.org.zd">http://www.zha.org.zd</a>.
<p>
</div>
<div id="highRisk">
<p>Your results show that you currently have a HIGH risk of
developing diabetes. [Your main risk factors are your BMI and
your diet.] We advise that you contact the Health Authority to
discuss your risk factors as soon as you can. Please fill in our
contact form and a member of the Health Authority Diabetes
Team will be in contact with you.
</div>
</section>
</body>

最佳答案

我删除了所有未使用的代码,只留下 addNumbers 函数。我还删除了每个按钮上的事件 - 没有这样的功能。

我总结了所有选中的单选按钮,因此如果页面上有其他单选按钮,您可能需要更具体的选择器。

function addNumbers() {
var selectedRadios = document.querySelectorAll('input[type="radio"]:checked');
var sum = 0;
for (var i = 0; i < selectedRadios.length; i++) {
sum += parseInt(selectedRadios[i].value);
}
document.getElementById('answer').value = sum;
var showDiv = sum < 15 ? "lowResult" : (sum < 25 ? "mediumResult" : "highRisk");
document.getElementById(showDiv).style.display = 'block';

}
.results {
display: none;
}
<table>

<tr>
<th scope="col"></th>
<th scope="col">noRisk</th>
<th scope="col">lowRisk</th>
<th scope="col">mediumRisk</th>
<th scope="col">HighRisk</th>
</tr>
<tr>
<th scope="row">
<div class="lefttext">How old are you?</div>
</th>

<td>
<input type="radio" id="value1" name="selectedAge" value="0" checked>1-25</td>
<td>
<input type="radio" id="value1" name="selectedAge" value="5">26-40</td>
<td>
<input type="radio" id="value1" name="selectedAge" value="8">41-60</td>
<td>
<input type="radio" id="value1" name="selectedAge" value="10">1-25</td>
</tr>

<tr>
<th scope="row">
<div class="lefttext">What is you BMI?</div>
</th>
<td>
<input type="radio" id="value2" name="selectedBmi" value="0" checked>0-25</td>
<td>
<input type="radio" id="value2" name="selectedBmi" value="0">26-30</td>
<td>
<input type="radio" id="value2" name="selectedBmi" value="9">31-35</td>
<td>
<input type="radio" id="value2" name="selectedBmi" value="10">35+</td>
</tr>
<tr>
<th scope="row">
<div class="lefttext">Does anybody in your family have diabetes?</div>
</th>
<td>
<input type="radio" id="value3" name="selectedDiabete" value="0" checked>No</td>
<td>
<input type="radio" id="value3" name="selectedDiabete" value="7">Grandparent</td>
<td>
<input type="radio" id="value3" name="selectedDiabete" value="15">Sibling</td>
<td>
<input type="radio" id="value3" name="selectedDiabete" value="15">Parent</td>
</tr>
<tr>
<th scope="row">
<div class="lefttext">How would you describe your diabete</div>
</th>
<td>
<input type="radio" id="value4" name="description" value="0" checked>Low sugar</td>
<td>
<input type="radio" id="value4" name="description" value="0">Normal sugar</td>
<td>
<input type="radio" id="value4" name="description" value="7">Quite high sugar</td>
<td>
<input type="radio" id="value4" name="description" value="10">High sugar</td>
</tr>
</table>
<input type="button" name="submit" value="Submit" onclick="javascript:addNumbers()" />Total =
<input type="text" id="answer" name="answer" value="" />



<section>
<h2>Your results:</h2>
<div class="results" id="lowResult">
<p>Your results show that you currently have a low risk of developing diabetes. However, it is important that you maintain a healthy lifestyle in terms of diet and exercise.
</p>
</div>
<div class="results" id="mediumResult">
<p>Your results show that you currently have a medium risk of developing diabetes. For more information on your risk factors, and what to do about them, please visit our diabetes advice website at <a href="http://www.zha.org.zd">http://www.zha.org.zd</a>.
<p>
</div>
<div class="results" id="highRisk">
<p>Your results show that you currently have a HIGH risk of developing diabetes. [Your main risk factors are your BMI and your diet.] We advise that you contact the Health Authority to discuss your risk factors as soon as you can. Please fill in our
contact form and a member of the Health Authority Diabetes Team will be in contact with you.
</div>
</section>

关于javascript - 使用 javascripting DOM 脚本隐藏 div,具体取决于单选按钮的总和,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31749440/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com