基本上,我正在编写一个HTML表单,它将验证一些数字(人工插入的土壤污染物浓度),并在最后的单元格中给出一条消息,根据这些数字所处的位置。
在我的例子中,土壤中铜的浓度为x=500 mg/kg。我需要用土壤污染的一些间隔(400
“你的土壤被认为被污染了”
例如,因为浓度超过400毫克/千克。
有人能告诉我怎么做吗?我需要把它整合到一个网页上。
发布于 2013-07-15 09:23:20
像这样吗?
window.addEventListener("load", function() {
document.getElementById("form1").addEventListener("submit", function(e) {
e.preventDefault(); // cancel submit
let val = this.copper.value;
const copper = isNaN(val) || val.trim() === "" ? 0 : +val; // convert to number
let text = "Please enter a numeric value";
if (copper > 0) {
text = "Your soil is ";
if (copper <= 400) text += "not polluted";
else if (copper < 1800) text += "considered polluted";
else text += "considered strongly polluted";
}
document.getElementById("soilMessage").innerHTML = text;
})
})<title>Soil analysis</title>
<form id="form1">
<input type="text" name="copper" id="copper" value="" />
<input type="submit" value=" Validate " />
</form>
<div id="soilMessage"></div>
https://stackoverflow.com/questions/17650577
复制相似问题