【发布时间】:2010-05-10 10:45:15
【问题描述】:
所以我正在使用 jQuery 日期选择器,它运行良好。我正在使用 AJAX 去获取一些内容,显然当应用这个新内容时,绑定丢失了,我 learnt about this last week 并发现了 .live() 方法。
但是如何将它应用到我的日期选择器?因为这不是一个事件,所以.live() 将无法提供帮助……对吧?
这是我用来将日期选择器绑定到我的输入的代码:
$(".datefield").datepicker({showAnim:'fadeIn',dateFormat:'dd/mm/yy',changeMonth:true,changeYear:true});
我不想在每次触发 AJAX 时都调用此方法,因为我希望尽可能保持通用性。
干杯:-)
编辑
按照@nick 的要求,下面是我的包装函数得到了ajax() 方法:
var ajax_count = 0;
function getElementContents(options) {
if(options.type===null) {
options.type="GET";
}
if(options.data===null) {
options.data={};
}
if(options.url===null) {
options.url='/';
}
if(options.cache===null) {
options.cace=false;
}
if(options.highlight===null || options.highlight===true) {
options.highlight=true;
} else {
options.highlight=false;
}
$.ajax({
type: options.type,
url: options.url,
data: options.data,
beforeSend: function() {
/* if this is the first ajax call, block the screen */
if(++ajax_count==1) {
$.blockUI({message:'Loading data, please wait'});
}
},
success: function(responseText) {
/* we want to perform different methods of assignment depending on the element type */
if($(options.target).is("input")) {
$(options.target).val(responseText);
} else {
$(options.target).html(responseText);
}
/* fire change, fire highlight effect... only id highlight==true */
if(options.highlight===true) {
$(options.target).trigger("change").effect("highlight",{},2000);
}
},
complete: function () {
/* if all ajax requests have completed, unblock screen */
if(--ajax_count===0) {
$.unblockUI();
}
},
cache: options.cache,
dataType: "html"
});
}
这个解决方案怎么样,我有一个 rules.js,其中包含我与元素的所有初始绑定,如果我要将它们放在一个函数中,然后在 ajax 方法的成功回调中调用该函数,这样我不会重复代码...
嗯,请思考:D
【问题讨论】:
-
你的 ajax 怎么样了,
$.ajax?您不重复代码的选项是使用上下文调用的函数或.liveQuery(),如果您有兴趣,我可以向您展示如何执行此操作,只需评论@nick并让我知道。跨度> -
嘿@nick,我已经编辑了我的原始帖子,另请参阅下面的可能解决方案... :-) 谢谢
-
谢谢 :) 在下面发布了关于如何解决此问题的答案
标签: binding datepicker jquery