jQuery的日期选择器不工作的AJAX生成的输入字段字段、日期、选择器、工作

2023-09-10 17:47:08 作者:缺德⒐⒋时尚

我有一个是通过Ajax从服务器侧生成的,并插入到当前页面的输入字段。我的问题是:jQuery的日期选择器不工作时通过Ajax生成它的输入栏,但它的工作原理,当现场直接放置在页面

I have an input field that is generated via Ajax from the server-side, and inserted into the current page. My problem is: the jQuery date picker is not working on the input field when it is generated via Ajax, but it works when the field is directly placed in the page.

下面,我包括我的code的缩小版本。

Below, I include a scaled-down version of my code.

HTML code:

HTML code:

<form id="user-form"></form>

和这里的jQuery的code,它是应该激活日期选择器就可以了。

And here's the jQuery code that's supposed to activate the datepicker on it.

$.ajax({
    type: "GET",
    url: "/inputfield-loader.php" ,
    success: function(data) {
        $('#user-form').html(data);
        $("#datefield").datepicker();
    } 
});

而这里的 inputfield-loader.php

<input type="text" name="firstname" id="firstname"></div>
<div><input type="text" name="email" id="email"></div>
<div><input type="text" name="birthdate" id="datefield"></div>
<div><input type="submit"></div>

正如我已经说过,这个效果很好,如果输入字段只是硬codeD插入页面。但是,当插入到DOM作为Ajax调用的返回字符串,日期选择器不再工作。

As I already said, this works well if the input field is just hard-coded into the page. But when inserted into the DOM as the return string of an Ajax call, the date picker no longer works.

任何人有任何想法如何解决这一问题?

Anybody has any idea how to fix this?

注意我已经更新了问题,包括codeS显示到底发生了什么,因为每个注释通过@AkshayKhandelwal

NOTE I have updated the question to include codes showing exactly what is happening, as per the comment by @AkshayKhandelwal

推荐答案

试试这样

success: function() {
  $('#datefield').datepicker();
}