简体   繁体   English

日期选择器的jQuery内联编辑

[英]jQuery Inline Editing for date picker

On my page, there are multiple places with date fields rendered as below; 在我的页面上,有多个地方,其日期字段呈现如下: So some date are always displayed in editable text boxes and other as labels (then inline-edited) 因此,某些日期总是显示在可编辑的文本框中,而其他日期则显示为标签(然后进行内联编辑)

<div class="editableDateTxt" title="06/04/2014">06/04/2011</div>
<input type="text" value="" placeholder="mm/dd/yyyy" name="date1" id="date1" class="datePicker">
<input type="text" value="" placeholder="mm/dd/yyyy" name="date2" id="date2" class="datePicker">

<div class="editableDateTxt" title="06/04/2014">06/04/2012</div>
<input type="text" value="" placeholder="mm/dd/yyyy" name="date3" id="date3" class="datePicker">

<div class="editableDateTxt" title="06/04/2014">06/04/2013</div>
<input type="text" value="" placeholder="mm/dd/yyyy" name="date4" id="date4" class="datePicker">

Now I need to implement inline editing...ie when label is clicked, I want to show the jQuery UI date picker and on blur, I want to hide... 现在我需要实现内联编辑...即单击标签时,我想显示jQuery UI日期选择器,而在模糊时,我想隐藏...

So I am using the below code; 所以我在用下面的代码;

$(document).on("click",".editableDateTxt", function () {
                        var input = $('<input />', {'type': 'text', 'class':'datePicker', 'value': $(this).html()});
                        $(this).parent().append(input);
                        $(this).remove();
                        input.focus();
                        $('.datePicker').datepicker().datepicker('show');
                });

                $(document).on("blur",".datePicker", function () {
                        $(this).parent().append($("<div class='editableDateTxt'/>").html($(this).val()));
                        $(this).remove();
                });

However, there seems to be an issue with the line; 但是,这条线似乎有问题。

$('.datePicker').datepicker().datepicker('show');

since there are multiple elements with class datePicker...How do I ensure that the datepicker is shown only where the div element with class "editableDateTxt" is clicked ? 由于存在多个具有datePicker类的元素...如何确保仅在单击具有“ editableDateTxt”类的div元素时显示datepicker?


Tried onSelect code 尝试过onSelect代码

I updated the code as below, but nothing happens on select (i even tried adding an alert inside onSelect, but it does not fire) 我更新了以下代码,但在选择上什么都没有发生(我什至尝试在onSelect内添加警报,但不会触发)

$(parent).find('.datePicker').datepicker().datepicker({
    onSelect: function(date) {
        parent.append($("<div class='editableDateTxt'/>").html(date));
        $(this).remove();
    }
}).datepicker('show');

Not sure if I understand your problem 100%. 不知道我是否100%理解您的问题。 But wouldn't just using $(this).find() solve your problem. 但是不仅仅使用$(this).find()解决您的问题。 See below: 见下文:

$(document).on("click",".editableDateTxt", function () { var input = $('', {'type': 'text', 'class':'datePicker', 'value': $(this).html()}); $(this).parent().append(input); $(this).remove(); input.focus(); $(this).find('.datePicker').datepicker().datepicker('show'); }); $(document).on(“ click”,“。editableDateTxt”,function(){var input = $('',{'type':'text','class':'datePicker','value':$ (this).html()}); $(this).parent()。append(input); $(this).remove(); input.focus(); $(this).find('。datePicker' ).datepicker()。datepicker('show');});

\n

I re-read your code. 我重新阅读了您的代码。 What about storing a reference to the parent and using that to search. 如何存储对父项的引用并使用该引用进行搜索。

$(document).on("click",".editableDateTxt", function () {
    var input = $('<input />', {'type': 'text', 'class':'datePicker', 'value': $(this).html()});
    var parent = $(this).parent();
    parent.append(input);
    $(this).remove();
    input.focus();
    $(parent).find('.datePicker').datepicker().datepicker('show');
});

Or do all the elements share the same parent. 或所有元素共享同一父对象。 Bit unsure as your JS seems to suggest unique parent elements, but your HTML doesn't. 有点不确定,因为您的JS似乎建议使用唯一的父元素,但是您的HTML却没有。 You may not have included all your HTML though. 您可能没有包括所有HTML。

Of course, just using the input variable would also work: 当然,仅使用输入变量也可以:

input.datepicker().datepicker('show');

Didn't notice that one at first. 一开始没注意到那个。


Responding to your comment about closing the datepicker and just showing the selected date. 回应您有关关闭日期选择器并仅显示所选日期的评论。 Best way for this is to use the onSelect option in the datepicker. 最好的方法是在日期选择器中使用onSelect选项。 Like this: 像这样:

parent.find('.datePicker').datepicker({
    onSelect: function(date) {
        parent.append($("<div class='editableDateTxt'/>").html(date));
        $(this).remove();
    }
}).datepicker('show');

See this working jsfiddle: http://jsfiddle.net/6pptH/ 看到这个工作的jsfiddle: http : //jsfiddle.net/6pptH/


So your entire JS becomes: 因此,您的整个JS变为:

$(document).on("click",".editableDateTxt", function () {
    var input = $('<input />', {'type': 'text', 'class':'datePicker', 'value': $(this).html()});
    var parent = $(this).parent();
    parent.append(input);
    $(this).remove();
    input.focus();
    parent.find('.datePicker').datepicker({
        onSelect: function(date) {
            parent.append($("<div class='editableDateTxt'/>").html(date));
            $(this).remove();
        }
    }).datepicker('show');
});

Instead of 代替

$('.datePicker').datepicker().datepicker('show'); 

try: 尝试:

input.datepicker().datepicker('show');

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

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