简体   繁体   English

jQuery添加时间到日期字段

[英]Jquery add time to date field

I am getting date from datepicker 我从日期选择器获取日期

$("#dt").datepicker({ dateFormat: 'MM dd yy' });

now, I want to add default time 8:00:00 AM to the date. 现在,我想为日期添加默认时间8:00:00。

var newdt =??? var newdt = ???

but not sure how, reason I didn't add time in the dateFormat is because I dont want to show the time on the UI. 但不确定如何,之所以没有在dateFormat中添加时间是因为我不想在UI上显示时间。

also how do I disable user from entering text in the textbox? 还如何禁止用户在文本框中输入文本? I want to allow the user to modify the date only using datepicker. 我想允许用户仅使用datepicker修改日期。

Thanks, 谢谢,

To disable entering text: 禁用输入文字:

function C_buttonNeutral(element) {
    element.onclick =           function() { return false; };
    element.onmousedown =       function() { return false; };
    element.onselectstart =     function() { return false; };
}
C_button_neutral(document.getElementById('dt'));

to add the time: 添加时间:

document.getElementById('dt').innerHTML += ' 8:00:00 AM';

Edit because of comment: 由于评论而编辑:

function changeTime(element, newTime) {
    // A time like this " 8:00:00 AM" has to whitespaces.
    element.innerHTML = element.innerHTML.substring(0, element.innerHTML.lastIndexOf(' '));
    element.innerHTML = element.innerHTML.substring(0, element.innerHTML.lastIndexOf(' '));

    // The new time is added
    element.innerHTML += ' ' + newTime;
}

//Example:
changeTime(document.getElementById('dt'), '10:00:00 AM');

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

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