JQuery Datepicker,无法手动触发onSelect事件! [英] JQuery Datepicker, can't trigger onSelect event manually!
问题描述
我正在使用jquery的datepicker,每当从内联datepicker对象中挑选一个日期时,会从ajax调用中填充项目列表。该脚本工作完美,除了我无法触发onSelect事件填充我的初始项目列表。
I am using jquery's datepicker where a list of items is populated from an ajax call whenever a date is picked from an inline datepicker object. The script works perfect except that I can't trigger an onSelect event to populate my initial list of items.
我可以解决这个问题,只是最初使用php,但我真的很想避免这种情况。
I could get around this problem by just populating the list initially using php but I would really like to avoid this.
$(document).ready(function(){
//create date pickers
$("#date_calendar").datepicker(
{
changeMonth: true,
changeYear: true,
dateFormat: 'yy-mm-dd',
defaultDate: $.datepicker.parseDate("y-m-d", $('#date').val()),
onSelect: function(dateText, inst)
{
alert('onSelect triggered! Yay!');
$('#date').val($.datepicker.formatDate("yy-mm-dd", $('#date_calendar').datepicker('getDate')));
// Ajax for populating days when selected
$.post(
"server_requests/show_day.php",
{
date: $('#date').val(),
user_id: $('#user_id').val()
},
function(data)
{
//return function
$('#my_day_tasks').html(data.resultTable);
},
"json"
);
}
}).disableSelection();
$("#date_calendar").trigger('onSelect');
});
任何帮助都赞赏:)
推荐答案
你不能将它重构为自己的功能,您重新使用它?严格来说,datepicker选择不是真的页面加载会发生什么。你只是想做完全同样的事情,当确实选择了datepicker。
Couldn't you just refactor that to a function of its own, which you reuse? Strictly speaking, a datepicker select is not really what happens on page load. You just want to do exactly the same thing that happens when the datepicker is indeed selected.
function populateList(dateText, inst)
{
alert('alert test');
$('#date').val($.datepicker.formatDate("yy-mm-dd",$('#date_calendar').datepicker('getDate')));
// Ajax for populating days when selected
$.post("server_requests/show_day.php",
{
date: $('#date').val(),
user_id: $('#user_id').val()
},
function(data)
{
//return function
$('#my_day_tasks').html(data.resultTable);
},
"json"
);
}
$(document).ready(function(){
//create date pickers
$("#date_calendar").datepicker(
{
changeMonth: true,
changeYear: true,
dateFormat: 'yy-mm-dd',
defaultDate: $.datepicker.parseDate("y-m-d", $('#date').val()),
onSelect: populateList
}).disableSelection();
// i'm not bothering to pass the input params here, because you're not using them anyway
populateList();
});
这篇关于JQuery Datepicker,无法手动触发onSelect事件!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!