简体   繁体   English

jQuery Datepicker不会覆盖动态创建的输入中的值,需要刷新页面

[英]jQuery Datepicker doesn't overwrite value in dynamically created input, page refresh needed

EDIT1: EDIT1:
So I did managed to fix this problem. 所以我设法解决了这个问题。 But I'm not sure how. 但是我不确定如何。 I've been using two identical forms with different IDs for different reasons (Add and Edit forms). 由于不同的原因,我一直在使用两个具有不同ID的相同表单(添加和编辑表单)。 In those forms I had inputs with IDs. 在那些表格中,我输入了带有ID的信息。 But for both forms I've been using the same ID for the inputs in them. 但是对于这两种形式,我一直在使用相同的ID作为输入。 So I change those IDs to classes (how it should be). 因此,我将这些ID更改为类(应该如何)。 I think this was the reason for the bug. 我认为这是该错误的原因。
The second thing I've changed was removing the first form in a moment I would click on the second form (Edit form/button) because It was making a trouble, trouble I didn't addresed in here and I don't think It has anything to do with my initial problem. 我更改的第二件事是立即删除第二个表单(单击编辑表单/按钮),因为这很麻烦,我没有在这里添加麻烦,我也不认为这很麻烦与我最初的问题有关。 Just writing everything I did in any case. 只要写我在任何情况下所做的一切。

I'm having a problem with jQuery Datepicker and Timepicker. 我在使用jQuery Datepicker和Timepicker时遇到问题。 I'll try to describe my situation. 我将尽力描述我的处境。
I have dynamically created html for my local storage values (imagine squares with it's own data in it.) 我已经为本地存储值动态创建了html(可以想象其中包含自己的数据的正方形。)

My problem... If I simply load these squares and I want to change something, by clicking on edit button, It will create a form with pre-written values in them, by clicking on one of these inputs, a calendar shows up. 我的问题...如果我只是加载这些正方形,并且想要通过单击“编辑”按钮来更改某些内容,它将通过单击这些输入之一来创建一个包含预写值的表单,并显示一个日历。 And then after clicking on some date, the pre-written value changes with value of a date I've clicked on. 然后,在单击某个日期后,预写的值将随着我单击的日期的值而更改。 Same for Timepicker. 时间选择器也一样。 But! 但! There is a problem. 有一个问题。

If I want to create new squares with new data ( by dynamically creating form with inputs, then the data is saved to local storage) and then (without refreshing the page) clicking on edit button, bringing same old form (as described above) and clicking on some of the inputs, calendar shows up, but after clicking on some date, the value of the input doesn't change. 如果我想用新数据创建新方块(通过使用输入动态创建表单,那么数据将保存到本地存储中),然后(无需刷新页面)单击“编辑”按钮,带来相同的旧表单(如上所述),然后单击某些输入,将显示日历,但是单击某个日期后,输入的值不会更改。 Same with Timepicker, doesn't overwrite the pre-witten value. 与Timepicker相同,不会覆盖pre-witten值。

But if I refresh the page and want to edit something (without creating new squares/data), Datepicker change the value without problem. 但是,如果我刷新页面并要编辑某些内容(而不创建新的正方形/数据),则Datepicker可以毫无问题地更改值。

Can you help me please? 你能帮我吗? I'll try to answer any question if needed. 如果需要,我将尝试回答任何问题。

Here is a function that show everything saved in local storage. 这是一个显示所有保存在本地存储中的功能。

function fetchBookmarks() {
var bookmarks = JSON.parse(localStorage.getItem("bookmarks"));
bookmarks.sort(function compare(a, b) {
    var dateA = new Date(a.date);
    var dateB = new Date(b.date);
    return dateB - dateA;
  });

var results = document.getElementById("results");
results.innerHTML = "";
for (var i = 0; i < bookmarks.length; i++) {
    var date = bookmarks[i].date;
    var distance = bookmarks[i].distance;
    var time = bookmarks[i].time;

    results.innerHTML += '<div class="bookmarks shadow p-3 m-2 bg-light rounded">' +
        '<div class="row">' +
        '<div class="col">' +
        '<h3>Date: </h3>' +
        '<h3>Distance: </h3>' +
        '<h3>Time: </h3>' +
        '</h3><input onclick="editBookmarks(\'' + time + '\')" class="btn btn-outline-primary mr-1 btn-lg" id="edit" type="button" value="Edit"><input onclick="deleteBookmarks(\'' + time + '\')" class="btn btn-outline-danger btn-lg" id="deleteBookmarks" type="button" value="Delete">' +
        '</div>' +
        '<div class="col">' +
        '<h3 class="font-weight-bold">' + date + '</h3>' +
        '<h3 class="font-weight-bold">' + distance + '</h3>' +
        '<h3 class="font-weight-bold">' + time + '</h3>'
    '</div>' +
        '</div>' +
        '</div>';
};

};

And here is function after clicking on edit button... 这是点击编辑按钮后的功能...

function editBookmarks(time) {
var bookmarks = JSON.parse(localStorage.getItem("bookmarks"));
for (var i = 0; i < bookmarks.length; i++) {
    if (bookmarks[i].time == time) {
        $(".bookmarks").hide();
        results.innerHTML += '<form class="bookmarks shadow p-3 m-2 bg-light rounded" id="editForm">' +
            '<h4>Date: </h4><input class="form-control form-control-lg" id="date" placeholder="Select" value="' + bookmarks[i].date + '" type=""><br>' +
            '<h4>Distance: </h4><input class="form-control form-control-lg" id="distance" placeholder="In miles" value="' + bookmarks[i].distance + '" type="text"><br>' +
            '<h4>Time: </h4><input class="form-control form-control-lg" id="time" placeholder="Select" value="' + bookmarks[i].time + '" type=""><br>' +
            '<input class="btn btn-success btn-lg" type="submit" value="Submit">' +
            '</form>';

        /* $('#date').datepicker()
        $('#time').timepicker({
            timeFormat: "H:mm",
            hourMin: 0,
            hourMax: 4
        }); */
        bookmarks.splice(i, 1);
    };
};


$("#editForm").on("submit", function (e) {
    var date = $("#date").val();
    var distance = $("#distance").val();
    var time = $("#time").val();

    if (!distance.length || !date.length || !time.length) {
        alert("Something missing!")
    } else {
        var bookmark = {
            date: date,
            distance: distance,
            time: time
        };

        bookmarks.push(bookmark);
        localStorage.setItem("bookmarks", JSON.stringify(bookmarks));

        fetchBookmarks();
        $("#editForm").hide();
    };
    e.preventDefault();
});
$("#search").hide();
};

And here is Datepicker with Timepicker 这是带有时间选择器的Datepicker

$('body').on('focus', "#date", function () {
$(this).datepicker();
});

$('body').on('focus', "#time", function () {
$(this).timepicker({
    timeFormat: "H:mm",
    hourMin: 0,
    hourMax: 4
});
});

I know that this is quite much to ask and I'm having a problem with articulating. 我知道这是很多问题,我在表达方面存在问题。 I'm just lost in this. 我只是迷路了。
I'm thankful for any help. 感谢您的帮助。

Here's a link for GitHub file, I think It's better than posting the code here. 这是GitHub文件的链接,我认为这比在此处发布代码要好。 My whole problem is at the bottom of the file, if you're interested. 如果您有兴趣,我的整个问题在文件的底部。
https://github.com/ovy1448/MyRun/blob/master/js/main.js https://github.com/ovy1448/MyRun/blob/master/js/main.js
Thanks again. 再次感谢。

As per my understanding, since you are using onfocus event to attach datepicker/timepicker, all the event of datepicker/timepicker is not getting attached to the field. 根据我的理解,由于您使用onfocus事件来附加日期选择器/时间选择器,因此日期选择器/时间选择器的所有事件都未附加到该字段。 I faced similar problem 2-3 years back. 2-3年前,我遇到了类似的问题。 What I did is that I added a function for the datepicker onSelect event which updates the field value. 我所做的是为datepicker onSelect事件添加了一个函数,该函数更新了字段值。 You can try this(as below) and let me know if it works 您可以尝试使用此方法(如下所示),让我知道是否可行

 $('body').on('focus', "#date", function () { $(this).datepicker({onSelect: function(dateStr){ $('body').('#date').val(dateStr); }}); }); $('body').on('focus', "#time", function () { $(this).timepicker({ timeFormat: "H:mm", hourMin: 0, hourMax: 4, onSelect: function(timeStr){ $('body').('#time').val(timeStr); } }); }); 

So I did managed to fix this problem. 所以我设法解决了这个问题。 But I'm not sure how. 但是我不确定如何。 I've been using two identical forms with different IDs for different reasons (Add and Edit forms). 由于不同的原因,我一直在使用两个具有不同ID的相同表单(添加和编辑表单)。 In those forms I had inputs with IDs. 在那些表格中,我输入了带有ID的信息。 But for both forms I've been using the same ID for the inputs in them. 但是对于这两种形式,我一直在使用相同的ID作为输入。 So I change those IDs to classes (how it should be). 因此,我将这些ID更改为类(应该如何)。 I think this was the reason for the bug. 我认为这是该错误的原因。
The second thing I've changed was removing the first form in a moment I would click on the second form (Edit form/button) because It was making a trouble, trouble I didn't addresed in here and I don't think It has anything to do with my initial problem. 我更改的第二件事是立即删除第二个表单(单击编辑表单/按钮),因为这很麻烦,我没有在这里添加麻烦,我也不认为这很麻烦与我最初的问题有关。 Just writing everything I did in any case. 只要写我在任何情况下所做的一切。

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

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