0

嗨,这让我发疯了。我不是开发人员。试图让这个工作。用户使用日期对象(日历)将日期(雇用日期)输入表单 下一个字段应采用该日期并减去今天的日期以获得就业时间。但是我在该领域变得不确定,并且我原来的雇用日期消失了。这是我所拥有的,请帮助,非常感谢!

//grab date of hire
try{document.getElementById("dData_DOH").onchange =          custom_calculateDate;}catch(e){}
//not sure if necessary - field that the difference should go to
try{document.getElementById("dData_LengthEmp").onblur =     insertDate;}catch(e){}

//Function to grab input hire date 
//Create variable for now
//Create variable for difference


function custom_calculateDate(){
 var hireDate = document.getElementById("dData_DOH").value = "";
     var timeNow= new Date();
 var diff = Math.abs(timeNow - hireDate);
document.getElementById("dData_DOH").setAttribute('value',     custom_calculateDate());
     }

//Function to get the difference into the LengthEmp field    
function insertDate() {
document.getElementById("dData_LengthEmp").setAttribute("",     custom_calculateDate());
}

我知道这是完全错误的,因为我说过我不是开发人员或程序员,我无法弄清楚如何将这些信息放入该字段并让我的原始字段仍然显示。

谢谢您阅读此篇!

4

2 回答 2

0

使用value代替setAttribute

document.getElementById("dData_DOH").value = custom_calculateDate();
于 2012-04-07T00:40:30.710 回答
0

哇哇哇。

我将尝试通过向您解释原因来重写您的代码:

// Firstly, the onchange event doesn't work on IE for text inputs, prefer onblur
document.getElementById('dData_DOH').onblur = function(e) {
    // Now let's get some variables
    var hireDate = this.value, // "this" refers to the element that triggered the event
        now = new Date(),
        // You were on the right track for the difference, almost!
        diff = Math.abs(new Date(now.getTime() - hireDate.getTime()))

    // Finally, just don't change the original field, but the one you wanted to modify
    document.getElementById('dData_LengthEmp').value = diff.toTimeString() // Get the time as a readable format
}

我还没有测试过这个解决方案,但它应该能让你走上正轨。

顺便说一句,不要使用 try/catch。

于 2012-04-07T00:59:23.820 回答