javascript - if-else 语句在 javascript/html 中不起作用
问题描述
我是 js 新手,任何帮助都将不胜感激。我创建了一个 javascript 函数,它从 HTML 表单中获取一些输入值,如下所示;
但是,在运行代码后,当我在单选按钮中选择女性时,javascript if 语句的 else 部分没有执行。我对男性的价值相同。我还创建了一个重置按钮,认为这可能会有所帮助,但仍然无法正常工作。请帮忙
<script type=text/javascript>
function calcCreatine(){
var sexInput = document.getElementsByName("sex")[0].value;
var ageInput = document.getElementsByName("patients-age")[0].value;
var weightInput = document.getElementsByName("patients-weight")[0].value;
var serumInput = document.getElementsByName("patients-serum")[0].value;
var result;
if (sexInput === "m") {
result = Math.round(((140 - ageInput) * weightInput * 1.23) / serumInput);
} else {
result = Math.round(((140 - ageInput) * weightInput * 1.04) / serumInput);
}
return alert(result + " mL/min - Creatinine clearance, original Cockcroft-Gault");
}
</script>
<form id="form-id">
<div id="creat-calc">
<div class="card">
<div class="card-header py-3">
<h6 class="m-0 font-weight-bold text-primary">Creatinine clearance calculator</h6>
</div>
<div class="card-body">
<p>Sex of patient:</p>
<div>
<label class="radio-inline">
<input type="radio" name="sex" value="m"> Male
</label>
<label class="radio-inline">
<input type="radio" name="sex" value="f"> Female
</label>
<p>Age of patient (years):</p>
<input type="number" min="1" max="120" name="patients-age" />
<br /><br />
<p>Weight of patient (kg):</p>
<input type="number" min="1" max="120" name="patients-weight" />
<br /><br />
<p>Serum creatinine (micromol/L):</p>
<input type="number" min="1" max="120" name="patients-serum" />
<br />
</div>
<br />
<hr />
<button type="button" class="btn btn-primary" onclick="calcCreatine();">Calculate</button>
<button type="button" class="btn btn-danger" onclick="popup.hideCeatCalcFormPopup();">Close</button>
<button type="button" class="btn btn-primary" onclick="myFunction()">Reset</button>
</div>
</div>
</div>
</form>
解决方案
试试这个
<script type=text/javascript>
function calcCreatine(){
var sexInput = document.querySelector('input[name="sex"]:checked').value;
;
var ageInput = document.getElementsByName("patients-age")[0].value;
var weightInput = document.getElementsByName("patients-weight")[0].value;
var serumInput = document.getElementsByName("patients-serum")[0].value;
var result;
console.log('sexInput', sexInput)
if (sexInput === 'm') {
result = Math.round(((140 - ageInput) * weightInput * 1.23) / serumInput);
} else {
result = Math.round(((140 - ageInput) * weightInput * 1.04) / serumInput);
}
return alert(result + " mL/min - Creatinine clearance, original Cockcroft-Gault");
}
</script>
<form id="form-id">
<div id="creat-calc">
<div class="card">
<div class="card-header py-3">
<h6 class="m-0 font-weight-bold text-primary">Creatinine clearance calculator</h6>
</div>
<div class="card-body">
<p>Sex of patient:</p>
<div>
<label class="radio-inline">
<input type="radio" name="sex" value="m"> Male
</label>
<label class="radio-inline">
<input type="radio" name="sex" value="f"> Female
</label>
<p>Age of patient (years):</p>
<input type="number" min="1" max="120" name="patients-age" />
<br /><br />
<p>Weight of patient (kg):</p>
<input type="number" min="1" max="120" name="patients-weight" />
<br /><br />
<p>Serum creatinine (micromol/L):</p>
<input type="number" min="1" max="120" name="patients-serum" />
<br />
</div>
<br />
<hr />
<button type="button" class="btn btn-primary" onclick="calcCreatine();">Calculate</button>
<button type="button" class="btn btn-danger" onclick="popup.hideCeatCalcFormPopup();">Close</button>
<button type="button" class="btn btn-primary" onclick="myFunction()">Reset</button>
</div>
</div>
</div>
</form>
推荐阅读
- scala - 如何过滤数据框以便只显示某些值?
- c# - XML 反序列化 - 索引和长度必须引用字符串中的位置
- jquery - 试图让选定的属性起作用
- google-apps-script - 应用程序制造商触发器从我的 2 个不同的电子邮件地址发送邮件两次
- mysql - 如果 id 不为空,则对 mysql 进行分组
- stm32 - STM32H7 上的 MDMA 和内部 FLASH R/W
- mongodb - 如何修改会话的过期超时
- asp.net - 在 Azure 应用程序日志中,32 位进程无法访问 64 位进程的模块
- php - 将 mssql datetime2 转换为 mysql datetime
- javascript - 如何封装 webcomponent 中使用的外部脚本