简体   繁体   English

jQuery UI日期选择器-在选择日期时更新另一个日期选择器

[英]jQuery UI date picker - updating another date picker on selecting a date

I have 2 date pickers using the Jquery UI. 我有2个使用Jquery UI的日期选择器。 If the user selects a date from date picker 1, how can I get it to update date picker 2 automatically with the same date? 如果用户从日期选择器1中选择一个日期,如何获取具有相同日期的自动更新日期选择器2?

I think this might be quite simple, but I've tried a few things and nothing seems to work. 我认为这可能很简单,但是我尝试了一些尝试,但似乎没有任何效果。

Thanks in advance. 提前致谢。

The date picker is essentially a text input element. 日期选择器本质上是一个文本输入元素。 You can use the jquery change handler on the first input element to grab its contents when they change, then simply select the second input element and update the value using jquery val. 您可以在第一个输入元素上使用jquery更改处理程序来捕获其更改时的内容,然后只需选择第二个输入元素并使用jquery val更新值。

http://api.jquery.com/change/ http://api.jquery.com/val/ http://api.jquery.com/change/ http://api.jquery.com/val/

Something like: 就像是:

$("#firstdatepicker").change(  function() {
   $("#seconddatepicker").val($(this).val());
});

You'll want to use the onClose event to update the second text field. 您将要使用onClose事件来更新第二个文本字段。

I've created this fiddle here that works; 我在这里创建了有效的小提琴 for some reason the styling that's usually on the datepicker is not coming in though. 由于某种原因,通常不会在datepicker上显示样式。

HTML: HTML:

<input type="text" name="date1" value="" />
<br>
<input type="text" name="date2" value="" />

Javascript: 使用Javascript:

$("input[name=date1]").datepicker({
    onClose: function(dateText, inst) {
        $("input[name=date2]").val(dateText);
    }
});
$("input[name=date2]").datepicker();

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

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