简体   繁体   English

从Webform上的2个输入字段计算总时间

[英]Calculate total time from 2 input fields on a Webform

I have a Webform where the user picks a start and end time from a drop list (I truncated the list here). 我有一个Webform,用户可以从下拉列表中选择开始时间和结束时间(我在此处将列表截断了)。 I want to have the total time calculated automatically. 我想自动计算总时间。 This is what I have so far and it works, however it does not submit to my DataBase. 到目前为止,这是我所拥有的并且可以正常使用,但是它不会提交给我的数据库。 However, if I change the span tag to an input tag it does work and submits however it will not work in Chrome, IE only. 但是,如果我将span标记更改为input标记,则它确实可以工作并提交,但仅在IE中的Chrome中不起作用。

 (function(d) { var start = d.getElementById('timeStart'), stop = d.getElementById('timeStop'), diff = d.getElementById('totaltime'); function textReplace(e, txt) { if (e.textContent) e.textContent = txt; else e.innerText = txt; } function addEvent(e, event, handler) { if (e.addEventListener) e.addEventListener(event, handler, false); else e.attachEvent('on' + event, handler); } function selectHours(e) { return new Date( '01/01/1971 ' + e.options[e.selectedIndex].value ).getTime(); } function calcTime(e) { d = new Date(selectHours(stop) - selectHours(start)); textReplace(diff, d.getUTCHours() + ':' + d.getUTCMinutes()); } addEvent(start, 'change', calcTime); addEvent(stop, 'change', calcTime); })(document); 
 <td style="height: 32px; width: 472px;"> <select name="start" id="timeStart" style="width:276px; color: black;background-color:#C1D7EB"> <option value="00:00:00">12:00 am</option> <option value="01:00:00">1:00 am</option> <option value="01:10:00">1:10 am</option> <option value="23:00:00">11:00 pm</option> </select> </td> <td style="height: 32px; width: 472px;"> <select name="end" id="timeStop" style="width:276px; color: black;background-color:#C1D7EB"> <option value="00:00:00">12:00 am</option> <option value="00:30:00">12:30 am</option> <option value="21:00:00">9:00 pm</option> <option value="22:00:00">10:00 pm</option> <option value="23:00:00">11:00 pm</option> </select> <td style="width: 472px"> Difference: <span id="totaltime" name="totaltime"></span> </td> 

If you change the totaltime span to an input, you need to modify your textReplace function as well. 如果将totaltime时间跨度更改为输入,则还需要修改textReplace函数。

To set the value of an input value, change the function to something similar to this: 要设置输入值的值,请将函数更改为与此类似的内容:

   function textReplace(e, txt) {
      e.value = txt;
   }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM