[英]How do I implement a “Submit” button on a Javascript/HTML calculation form?
I want to add a "Submit" button in order to execute the formula after submit is pressed, but I can't seem to connect the submit button to the code to make the total emissions update only when the button is pressed. 我想添加一个“提交”按钮以便在按下提交后执行公式,但是我似乎无法将提交按钮连接到代码以仅在按下按钮时才更新总排放量。
When you input a vehicle type and miles driven and press somewhere else on the page, the total emissions updates. 当您输入车辆类型和行驶里程并按页面上的其他位置时,总排放量将更新。 I'm trying to implement a 'submit' or 'calculate' button so that the total emissions only updates when the button is clicked. 我正在尝试实现“提交”或“计算”按钮,以使总排放量仅在单击按钮时更新。
I'd also like to wrap the Javascript in a function rather than window.onload, but I'm not sure how to implement that either. 我也想将Javascript包装在一个函数中,而不是在window.onload中,但是我不确定如何实现它。
window.onload = function () { var carType = document.getElementById('VehicleType'); var total = document.getElementById('totalEmissions'); var miles = document.getElementById('milesDriven'); carType.onchange = function(){ if(miles.value){ total.value = parseFloat(carType.value) * parseFloat(miles.value); } if(!miles.value || !carType.value){ total.value = ""; } }; miles.onchange = function () { if(miles.value && carType.value){ total.value = parseFloat(carType.value) * parseFloat(miles.value) } if(!miles.value || !carType.value){ total.value = ""; } }; };
<strong>Car Type</strong></th><br> <select name="VehicleType" id="VehicleType"> <option value="">Select an option…</option> <option value="552">Sprinter Van</option> <option value="444">Personal Car</option> <option value="444">Renter Car</option> <option value="140">Bus or Shuttle</option> <option value="727">Diesel Truck</option> <option value="683">Regular Truck</option> </select> <br><br> <strong>Miles Driven</strong></th><br> <td width="65"><input type="text" name="milesDriven" id="milesDriven" size="15" maxlength="5"/></td> <td width="43">miles</td> <br><br> <strong>Total CO2 Emissions (grams)</strong><br> <td><input type="text" name="totalEmissions" id="totalEmissions" size="15" maxlength="5"/></td>
What you need is not a submit button, but a regular <button>
element. 您需要的不是提交按钮,而是常规的<button>
元素。 Just create one, give it an id
, then listen for its click
event, instead of change
events of the other two elements. 只需创建一个,给它一个id
,然后侦听其click
事件,而不是其他两个元素的change
事件。 Check for validity inside the function that you execute when it's clicked and show the result, like this: 在单击函数时检查所执行函数的有效性,并显示结果,如下所示:
window.onload = function() { var carType = document.getElementById('VehicleType'); var total = document.getElementById('totalEmissions'); var miles = document.getElementById('milesDriven'); var calculateButton = document.getElementById('calculateButton'); calculateButton.addEventListener('click', calculate); function calculate() { if (miles.value && carType.value) { total.value = parseFloat(carType.value) * parseFloat(miles.value); } if (!miles.value || !carType.value) { total.value = ""; } }; };
<strong>Car Type</strong><br> <select name="VehicleType" id="VehicleType"> <option value="">Select an option…</option> <option value="552">Sprinter Van</option> <option value="444">Personal Car</option> <option value="444">Renter Car</option> <option value="140">Bus or Shuttle</option> <option value="727">Diesel Truck</option> <option value="683">Regular Truck</option> </select> <br><br> <strong>Miles Driven</strong><br> <input type="text" name="milesDriven" id="milesDriven" size="15" maxlength="5" /> miles <br><br> <strong>Total CO2 Emissions (grams)</strong><br><input type="text" name="totalEmissions" id="totalEmissions" size="15" maxlength="5" /> <br><br> <button id="calculateButton">Calculate</button>
Note 1: As @ScottMarcus pointed out in the comments, instead of using onXyz
, it's better to use .addEventListener(eventName, callback)
. 注意1:正如@ScottMarcus在注释中指出的那样,最好使用onXyz
.addEventListener(eventName, callback)
而不是onXyz
。
Note 2: I would suggest rechecking your HTML, if this is not part of something bigger, as there are a few problems I can see (which I fixed in my snippet). 注意2:如果这不是更大的内容的一部分,我建议重新检查您的HTML,因为我可以看到一些问题(已在代码段中修复)。 The W3C Markup Validation Service is a great place to check for problems in your markup. W3C标记验证服务是检查标记问题的好地方。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.