我有一个链接下拉框的jQuery函数:
function updateCargoDestinations() {
$.ajax({
url: "/truckingmanagement/account/getAccountUserCargoDestinations",
data: "id=" + this.value,
cache: false,
success: function(html) {
$("#cargoDestination").html(html);
}
});
}
$(document).ready(function() {$("#account").change(updateCargoDestinations);});
它工作得很好,但我也需要这个函数运行一次,当页面加载,所以链接下拉框将正确填充。我尝试在函数中设置上下文,如下所示:
context: ("#account")
但这不起作用。我怎么能使它,所以这个函数运行一次,当页面加载和运行时,观察到的下拉框发生变化?
在变量中设置this.value
,然后检查undefined
。如果undefined
设置为初始填充下拉菜单所需的任何值。
function updateCargoDestinations() {
var selectId = this.value;
if (typeof selectId === "undefined") {
selectId = "whatever_you_need_here";
}
$.ajax({
url: "/truckingmanagement/account/getAccountUserCargoDestinations",
data: "id=" + selectId,
cache: false,
success: function(html) {
$("#cargoDestination").html(html);
}
});
}
$(document).ready(function() {
updateCargoDestinations();
$("#account").change(updateCargoDestinations);
});
查看未定义检查:http://jsfiddle.net/kfJvA/1/