jQuery Datepicker 和 Timepicker 用于同一输入字段依次弹出

发布于 2024-07-24 12:01:13 字数 1228 浏览 2 评论 0原文

我在 2 个单独的输入字段中有一个日期选择器和一个时间选择器,但我需要将 2 个字段输入合并为一个以进行数据库调用。 想知道两个控件是否只能使用 1 个输入字段?

首先调用日期选择器,用户单击所需的日期并输入值,然后时间选择器将弹出并附加用户在第一个操作中选择的日期。

我正在使用 jQuery UI DatepickerTimepickr 插件

所以我有这样的东西

<input class="datepicker">2009-06-22</input>
<input class="timepicker">12:00:00</input>

但想要这样的东西

<input class="datetimepicker">2009-06-22 12:00:00</input>

<script type="text/javascript">
// Date and Time picker same input field
$("#datepicker").datepicker( {
   dateFormat: 'yy-mm-dd',
   onClose: function(dateText, inst) {
      var tempDate = $("#datepicker").val(dateText);
      $("#datepicker").timepickr({
         onClose: function(timeText, inst) {
            var tempTime = $("#datepicker").val(dateText);  
            alert("Temp Time: '" + tempTime.val() + "'");
            $("#datepicker").val(tempDate.val() + ' ' + tempTime.val());
         }  
      });                                   
   }
});             
</script>

I have a Datepicker and a Timepicker in 2 separate input fields but I need to combine the 2 fields inputs into one for a database call. Wanted to know if I could only use 1 input field for both controls?

first call the datepicker and the user would click the date wanted and it would enter the value, then the timepicker would popup and append the date the user selected in the first action.

I'm using the jQuery UI Datepicker and Timepickr plug-ins

So I have something like this

<input class="datepicker">2009-06-22</input>
<input class="timepicker">12:00:00</input>

But would like something like this

<input class="datetimepicker">2009-06-22 12:00:00</input>

<script type="text/javascript">
// Date and Time picker same input field
$("#datepicker").datepicker( {
   dateFormat: 'yy-mm-dd',
   onClose: function(dateText, inst) {
      var tempDate = $("#datepicker").val(dateText);
      $("#datepicker").timepickr({
         onClose: function(timeText, inst) {
            var tempTime = $("#datepicker").val(dateText);  
            alert("Temp Time: '" + tempTime.val() + "'");
            $("#datepicker").val(tempDate.val() + ' ' + tempTime.val());
         }  
      });                                   
   }
});             
</script>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

扛起拖把扫天下 2024-07-31 12:01:13

我想推荐 Any+Time(TM),您可以在其中组合日期/时间在单个字段中使用单个、易于使用(且快速!)的日期/时间组合选择器。

I'd like to recommend Any+Time(TM), in which you can combine the date/time in a single field with a single, easy-to-use (and quick!) date/time combo picker.

爱本泡沫多脆弱 2024-07-31 12:01:13

您可以将事件绑定到日期选择器的 onClose() 事件。 让它打开你的时间选择器。 让日期选择器写入输入的基值,并让时间选择器将其值后面的空格附加到同一输入。

  1. 将日期选择器绑定到输入。
  2. 将打开时间选择器的方法附加到日期选择器的 .onClose() 事件。
  3. 将时间选择器值(前面带有空格)附加到输入值。

更新:以下是更新问题的更新结果。

该文档给出了一个关于如何将事件/操作绑定到日期选择器的关闭的简短示例:

$('.selector').datepicker({
   onClose: function(dateText, inst) { ... }
});

在此示例中,您可以使用“onClode:”后面的函数将值写入输入,并弹出时间选择器。

$(".selector").datepicker({
  onClose: function(dateText, inst) {
    $(".selector").val(dateText);
    // Raise Timepickr
  }
});

You could bind an event to the onClose() event of the datepicker. Have it open up your time picker. Let the datepicker write the base value of the input, and have the timepicker append it's value following a space to the same input.

  1. Bind date-picker to input.
  2. Attach method to open timepicker to .onClose() event of date-picker.
  3. Append time-picker value (with a preceding space) to input value.

Updated: The follow is an updated resulting from an updated question.

The documentation gives a brief example on how to bind an event/action to the closing of the datepicker:

$('.selector').datepicker({
   onClose: function(dateText, inst) { ... }
});

In this example, you could use your function following "onClode: " to write the value to the input, and popup the timepicker.

$(".selector").datepicker({
  onClose: function(dateText, inst) {
    $(".selector").val(dateText);
    // Raise Timepickr
  }
});
谁的新欢旧爱 2024-07-31 12:01:13

不是 jQuery,但它非常适合带有时间的日历:JavaScript 日期时间选择器

只需绑定点击事件即可弹出它:

$("#date").click(function() {
    NewCssCal($(this).attr('id'), 'mmddyyyy', 'dropdown', true, 12);
});

就我个人而言,我发现这更容易理解。 但显然你有很多选择,有多个 jquery 插件可以完成你的工作。

Not jQuery, but it works well for a calendar with time: JavaScript Date Time Picker.

Just bind the click event to pop it up:

$("#date").click(function() {
    NewCssCal($(this).attr('id'), 'mmddyyyy', 'dropdown', true, 12);
});

Personally I find this much easier to follow. But obviously you have so many options with multiple jquery plugins available to do your job.

病女 2024-07-31 12:01:13

您可以尝试将两个控件的输入合并为一个,例如

$("#datetime").focus(function() {
    var mydate = $("#date").val();
    var mytime = $("#time").val();
    if(mydate && mytime && !this.value) {
        this.value = mydate + " " + mytime;
    }
});

You can try combining the inputs from both controls into one like

$("#datetime").focus(function() {
    var mydate = $("#date").val();
    var mytime = $("#time").val();
    if(mydate && mytime && !this.value) {
        this.value = mydate + " " + mytime;
    }
});
亢潮 2024-07-31 12:01:13

这是一个替代方案:

http://www.r-ip.ru/dev/日期时间选择器/index.html

$('#datetimepicker').datetimepicker({ 
  dateFormat: 'yy-mm-dd',
  timeFormat: ' hh:ii:ss' 
});

Here's an alternative:

http://www.r-ip.ru/dev/datetimepicker/index.html

$('#datetimepicker').datetimepicker({ 
  dateFormat: 'yy-mm-dd',
  timeFormat: ' hh:ii:ss' 
});
醉酒的小男人 2024-07-31 12:01:13

请查看链接

我更新了一些布局问题并添加了第二个滑块,如果 {showTime:true, time24h:true} 则该

滑块将处于活动状态。您可以在 Google 代码

Please checkout the link.

I have updated some layout issue and added second slider, which will be active if {showTime:true, time24h:true}

You can find the original one at Google Code.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文