日期选择器的AJAX格重装后无法启动 [英] datepicker wont start after ajax div reload
问题描述
我有一个是通过AJAX更新一个div里面一个jQuery日期选择器。我能整个页面重新加载后打开日期选择器一次,但阿贾克斯只更新一个div,其中包括日期选择器输入后,我无法看到日期选择器。我已经试过,我伤了类似的问题的解决方案,没有运气。
I have a jquery datepicker inside a div that is updated via ajax. I'm able to open datepicker once after whole page reloads, but after ajax updates only a div that includes datepicker input, I'm not able to see datepicker. I have tried a solutions that I wound for similar problems, with no luck.
我不是很熟悉jQuery和AJAX,所以也许有人可以看到这个问题立刻。
I'm not very familiar with jquery and ajax, so maybe someone can see the problem immediatly.
我有一个jQuery的功能,包括AJAX:
I have a jquery function that includes ajax:
function example(){
$.ajax({
url: "page.php",
cache: false,
success: function(data){
$('#div').html(data);
}
});
}
阿贾克斯重新加载一个div,其中有一个日期选择器里面输入, 它看起来是这样的:
Ajax reloads a div, which has a datepicker input inside, which looks like this :
<input type="text" id="datepicker"/>
日期选择器的构造是这样的:
Datepicker is constructed this way:
$(function() {
$("#datepicker").datepicker({dateFormat: "dd.mm.yy"});
$.datepicker.setDefaults(
$.extend(
{'dateFormat':'yy-mm-dd'},
$.datepicker.regional['fi']
)
);
});
我有什么做的,要能看到日期选择器通过AJAX每次格更新? 有些人建议日期选择器是刷新在阿贾克斯成功,我没有得到它的工作这种方式,或者不知道怎么办!
What do I have to do, to be able to see datepicker every time div updates via ajax? Some suggested datepicker to be refresh at ajax success, I didnt get it working this way, or didnt know how!
推荐答案
这样做将是重新运行 .datepicker()对阿贾克斯的成功
初始化的方法之一。
One way to do it would be to rerun the .datepicker()
initializer on ajax success.
更改日期选择器code到:
Change the datepicker code to:
function applyDatepicker(elem) {
$(elem).datepicker({dateFormat: "dd.mm.yy"});
//... Your datepicker code
}
然后,一旦在文件准备就绪:
Then once on document ready:
$(document).ready(function(){
applyDatepicker('#datepicker');
})
一旦在阿贾克斯成功的回调:
And once in your ajax success callback:
$.ajax({
//... other ajax params,
success: function(){
//... success callback code
applyDatepicker('#datepicker');
}
});
另外, @adrenalin 表明,只有这样才能计算的最佳做法是更多地了解你的$ C $℃。
Also, as @adrenalin suggests, the only way to figure the best practice would be to know more about your code.
一个建议是更改的元素有一个类=日期选择器
,而不是 ID
的主要的原因,有可能是在一个页面和id的多个datepickers总是意味着是唯一的。作为一个初学者,我建议最简单的方法就是尽量想象一个ID为元素的专有名词,而类将是它的通用名词或形容词!
A suggestion would be to change the element to have a class="datepicker"
rather than an id
for the main reason that there could be multiple datepickers in a page and id's are always meant to be unique. As a beginner, I suggest the easiest approach is to try to imagine an ID as an element's Proper Noun whereas Class will be it's Common Noun or Adjective!
这篇关于日期选择器的AJAX格重装后无法启动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!