且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何将参数传递给 jQuery $.getJSON 回调方法?

更新时间:2023-11-03 19:26:04

不需要传入,直接引用你已有的变量,像这样:

You don't need to pass it in, just reference the variable you already have, like this:

var locationType = 3;
var url = 'blah blah blah' + '&locationType=' + locationType;
$("#loading_status").show();
$.getJSON(url, null, function(results) {
    searchResults(results, locationType)
});

此外,如果您没有数据对象,则无需传递 null,它是一个可选参数,jQuery 会检查第二个参数是否为函数,因此您可以这样做:

Also there's no need to pass null if you don't have a data object, it's an optional parameter and jQuery checks if the second param is a function or not, so you can just do this:

$.getJSON(url, function(results) {
    searchResults(results, locationType)
});