JavaScript - 输入日期,然后自动填充下一个字段的差异
JavaScript - Input a date then autopopulate next field with difference
嗨,这让我发疯了。我不是开发人员。试图让它工作。用户使用 Date 对象(日历)将日期(雇用日期)放入表单中下一个字段应采用该日期并减去今天的日期以获得工作时长。但是我在现场没有定义,我原来的雇用日期消失了。这是我所拥有的,请帮忙,非常感谢!
//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());
}
我知道这是完全错误的,正如我所说,我不是开发人员或程序员,我无法弄清楚如何将这些信息放入该领域并让我的原始字段仍然显示。
感谢您阅读本文!
使用 value
而不是 setAttribute
document.getElementById("dData_DOH").value = custom_calculateDate();
哇
哇哇。
我将尝试通过解释原因来重写您的代码:
// 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
}
我还没有测试过解决方案,但它应该让你走上正轨。
顺便说一句,不要使用尝试/捕获。
相关文章:
- 滚动到容器中的下一个元素-几乎到了
- 后焦点更改为IE 11中的地址栏,而不是转移到表单中的下一个控件
- 使用CSS或js,使用动态选择器选择任意li的下一个元素
- 使用ajax将数据从一个步骤发送到下一个步骤的3步表单
- AngularJS&JSON-从Previous&下一个对象
- 使用向下箭头键(与tab键一样)聚焦下一个输入
- 来自文档或下一个静态父级的事件委派
- angularjs移除焦点上的活动类并添加到下一个项目
- 禁用旋转木马中的下一个按钮和上一个按钮
- 当按下一个键时,请多次按下不同的键
- 设置一个下拉填充到它's的值和文本在AngularJS和asp.net webApi中的编辑模式
- 想要禁用我的下一个按钮,直到使用 JavaScript 填充我的全部选项
- JavaScript - 输入日期,然后自动填充下一个字段的差异
- 用数据库的输入填充下拉框,从而导致另一个填充的页面
- 如何隐藏质量上的下一个按钮,直到文本字段被文本填充
- 当填充JavaScript时,更改为下一个文本区域
- 当填充4个字符时,自动制表到下一个输入字段
- 如何在填充4个字符后移动下一个输入TAB
- 突出显示下一个要填充的单选按钮组
- 在使用next()填充时显示下一个输入字段