简体   繁体   English

如何在与Datepicker相同的输入字段中用Javascript编写可编辑的时间代码,但不使用Datetimepicker

[英]How can I write a editable time code in Javascript in the same input field as Datepicker, but without using Datetimepicker

I myself wrote this particular code to get the current time: 我自己编写了以下特定代码以获取当前时间:

$(function() {
    function getCurrentTime() {
        var CurrentTime = "";
        try {
            var CurrentDate = new Date();
            var CurrentHours = CurrentDate.getHours();
            var CurrentMinutes = CurrentDate.getMinutes();
            var CurrentSeconds = CurrentDate.getSeconds();

            if (CurrentMinutes < 10) { CurrentMinutes = "0" + CurrentMinutes; }
            if (CurrentSeconds < 10) { CurrentSeconds = "0" + CurrentSeconds; }
            if (CurrentHours < 10) { CurrentHours = "0" + CurrentHours; }

            CurrentTime = "" + CurrentHours + ":" + CurrentMinutes + ":" + CurrentSeconds + "";
        }
        catch (ex) {
        }
        return CurrentTime;
    }

and the only way I managed to show in the same input field as datepicker was this: 我设法在与datepicker相同的输入字段中显示的唯一方法是:

$(".class").datepicker({
    autoclose: true,
    format: "dd.mm.yyyy" + " " + getCurrentTime(),
}

This is all fine and well, but I cannot edit the content of the time, as when the datepicker closes it always returns the current time. 一切都很好,但是我不能编辑时间的内容,因为当日期选择器关闭时,它总是返回当前时间。 Please help my find a way around this, WITHOUT using another plug in. 请帮助我找到解决此问题的方法,而无需使用其他插件。

Not sure I understood what your goal is. 不确定我了解您的目标是什么。 Should the user input manually the time? 用户应该手动输入时间吗?

This change in the getCurrentTime function will check if in the datepicker current value there is a "time" part - if there is such time part, it will use that text instead of recomputing it. getCurrentTime函数中的此更改将检查datepicker当前值中是否有“时间”部分-如果有这样的时间部分,它将使用该文本而不是重新计算它。

Could be buggy since I've had no chance to try it, but this could be a possible solution. 由于我没有机会尝试,因此可能会出现故障,但这可能是一种解决方案。 Not the best since using that "format" property isn't the best way to achieve this, but it should work. 这不是最好的方法,因为使用该“格式”属性不是实现此目的的最佳方法,但它应该可以工作。

$(function() {
    function getCurrentTime() {
        var CurrentTime = "";
        try {
            var currTime = undefined;
            var currVal = $('#my-datepicker').val();
            if (currVal.length > 11) {
                currTime = currVal.substring(11, 8);
            }
            if (typeof currTime !== 'undefined' && currTime.length == 8) {
                CurrentTime = currTime;
            } else {


                var CurrentDate = new Date();
                var CurrentHours = CurrentDate.getHours();
                var CurrentMinutes = CurrentDate.getMinutes();
                var CurrentSeconds = CurrentDate.getSeconds();

                if (CurrentMinutes < 10) { CurrentMinutes = "0" + CurrentMinutes; }
                if (CurrentSeconds < 10) { CurrentSeconds = "0" + CurrentSeconds; }
                if (CurrentHours < 10) { CurrentHours = "0" + CurrentHours; }

                CurrentTime = "" + CurrentHours + ":" + CurrentMinutes + ":" + CurrentSeconds + "";
            }
        }
        catch (ex) {
        }
        return CurrentTime;
    }

IF i understand the problem correctly, HTML5 input type "date" is one of easiest solution which work without any plugin. 如果我正确理解问题,则HTML5输入类型“日期”是最简单的解决方案之一,无需任何插件即可工作。 The <input type="date"> is used for input fields that should contain a date. <input type="date">用于应包含日期的输入字段。

example : <input type="date" name="bday" min="2000-01-02"> 示例: <input type="date" name="bday" min="2000-01-02">

One similar post with solution is there Input type DateTime - Value format? 一种类似的解决方案是输入类型DateTime-值格式?

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

相关问题 如何将金额输入字段设置为不可编辑和可编辑我正在使用 angularJS 编程语言 - How can i set amount input field as non-editable and editable i am using angularJS programming language 如何使html输入字段的占位符文本可编辑? - How can I make the placeholder text of an html input field editable? 如何在 javascript 中编写没有 for 循环的以下代码 - How can i write the following code without for loops in javascript 使输入字段jquery ui datepicker可编辑 - Make input field jquery ui datepicker editable 如何清除datetimepicker上的输入文本? - How can I clear input text on datetimepicker? 如何在不使用框架的情况下编写面向对象的Javascript? - How can I write object oriented Javascript without using a framework? 如何仅使用JavaScript在输入字段中拆分数据? - How can I split data in an input field using only JavaScript? 如何在javascript中使用此键编写代码来提取值? - How can I write code using this key in javascript to extract the value? 如何使用jQuery-UI datepicker获取表单输入字段以使用其他字段中的年份? - How can I get a form input field using jQuery-UI datepicker to use the year from another field? 如何在不同时运行的情况下将相同的 Javascript 代码分配给多个 div - How do I assign the same Javascript code to multiple divs without running it at the same time
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM