简体   繁体   English

结合两个javascript函数计算表格

[英]Combining two javascript functions to calculate form

I have a form that calculates the time between two dates, and stores 6 numeric fields.我有一个表格可以计算两个日期之间的时间,并存储 6 个数字字段。

I wnat to be ale to have the form claculate these values as the user starts selecting after the two dates so that the form works as intended.当用户在两个日期之后开始选择时,我希望让表单计算这些值,以便表单按预期工作。

A sample page is located here..of what my intentions are示例页面位于此处..我的意图是什么

http://jsfiddle.net/GfN5u/1/ the js is withing the html http://jsfiddle.net/GfN5u/1/ js与html

I feel i'm almost there but lifes not fair to me this week.我觉得我快到了,但这周的生活对我来说并不公平。

Also in IE 8 the select elements lose their content.....even in jsfiddle.同样在 IE 8 中,select 元素丢失了它们的内容.....即使在 jsfiddle 中也是如此。

Any help would be appreciated.任何帮助,将不胜感激。

Thank you谢谢

        <script>
        var txt1 = document.Edit.StartTime.value
        var txt2 = document.Edit.EndTime.value

        var differenceInSeconds = (Calendar.parseDate(txt2, false).getTime() - Calendar.parseDate(txt1, false).getTime())/1000;
        var val9 = differenceInSeconds/60.0/60
        alert(val9)
        </script>


            <script>
        re=/\D/g

        function getGoodVal(fld) {
        var val = fld.value;
        if (!val) val=0;
        if (isNaN(val)) {
        val=val.replace(re,""); // remove all nonDigits
        }
        if (val=="" || isNaN(val)) val = 0; // still not a number or empty
        val = parseFloat(val)
        fld.value=val.toFixed(2);
        return val;
        }

        function calc(theForm) {
        var val10 = val9
        var valb = getGoodVal(theForm.lunch);   
        var val1 = getGoodVal(theForm.TRDO);
        var val2 = getGoodVal(theForm.AnnualLeave);
        var val3 = getGoodVal(theForm.PersonalLeave);
        var val4 = getGoodVal(theForm.WorkFromHome);
        var val5 = getGoodVal(theForm.TOIL); 

        total = (val9-valb) + (val1+val2+val3+val4+val5);
        theForm.total.value=total.toFixed(2)
        }
        </script>   

To calculate the values as the user selects dates, link your calculation to some javascript event that is called after a date is selected.要在用户选择日期时计算值,请将您的计算链接到在选择日期后调用的某些 javascript 事件。 I noticed that you attempted to link your calc() function to the onChange event of the text inputs, which does not work.我注意到您试图将您的 calc() function 链接到文本输入的 onChange 事件,这不起作用。 You can link the calc() function to the onSelect property of Calendar.setup.您可以将 calc() function 链接到 Calendar.setup 的 onSelect 属性。 It will look something like this:它看起来像这样:

  Calendar.setup({
    inputField : "StartTime",
    trigger    : "f_btn1",
    onSelect   : function() { this.hide(); calc(document.forms[0]); },
    showTime   : 12,
    dateFormat : "%Y-%m-%d %I:%M %p"
  });

Notice that calc() is called just like the onChange property of the input element.请注意,调用 calc() 就像输入元素的 onChange 属性一样。 However, the reference to the form changes because the form is not part of the Calendar scope: document.forms[0].但是,对表单的引用会发生变化,因为该表单不是日历 scope:document.forms[0] 的一部分。

It would probably be helpful to your users to have a function that verifies that updates the end time so that it cannot be earlier than the start time.拥有一个 function 可能会对您的用户有所帮助,它可以验证是否更新了结束时间,以便它不能早于开始时间。

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

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