我有以下代码:JavaScript变量不更新
function showDatePicker(){
var isEnabled=#{bean.isEnabled()};
if (isEnabled){
jQueryForDatepicker(".calendar").datepicker('enable');
jQueryForDatepicker(".calendar").datepicker({
changeMonth: true,
changeYear: true,
showOn: "button",
dateFormat:'yy-mm-dd',
buttonImage: "../resources/calendar.gif",
buttonImageOnly: true,
buttonText: "Select date",
beforeShow: function (input, inst) {
var rect = input.getBoundingClientRect();
setTimeout(function() {
inst.dpDiv.css({ marginTop: -input.offsetHeight + '10' , marginLeft: input.offsetWidth + '10' });
}, 0);
}
});
} else {
jQueryForDatepicker(".calendar").datepicker('disable');
}
}
的功能是从一个Ajax事件调用,就是绑在下拉列表中。该下拉列表包含“启用”,“禁用”值,并设置托管bean中的属性。我的问题是,在javascript函数中,var isEnabled仅在加载页面时设置。最初设置为false,之后,如果我在下拉菜单中更改该值,那么即使我的bean发回正确的值,javascript中的变量也不会更新。因此,如果它加载了错误的值,即使它应该更新为true,反之亦然,它仍然保持这种状态。
每次下拉改变时都会触发函数,这里有一个ajax调用,唯一的问题是变量保持不变在原来的方式是阅读。没有得到更新.. – att1la
那是因为你是从后端或服务器声明它。 从前端声明它:) – knaos