【发布时间】:2016-12-09 07:03:08
【问题描述】:
我试图弄清楚如何传递我的回调附加参数。到目前为止,我有以下代码。
$("#refine_mp_type").change(function(){
var mp_type = $(this).val();
var country_id = $('#refine_country').val();
var dropdown = $('#refine_grade').find('select');
getMPTypeGrades(country_id, mp_type, populateGradesDropdown);
});
function getMPTypeGrades(country_id, mp_type, callback){
display_wait_dialog ("Please Wait", "Loading grades...");
$.ajax(
{
//do ajax
})
.done(function(data){
callback(data);
})
.fail(//do something)
.always(//do something);
}
populateGradesDropdown(data){
//populate the dropdown with the returned data
}
到目前为止,我对如何实现这一点的想法是:
- 我可以将下拉变量设置为全局范围并以这种方式访问它(会污染全局范围)。
- 我可以将下拉元素传递给 getMPTypeGrades 函数,然后将其传递给回调函数。 (我想让 getMPTypeGrades 尽可能通用。我不知道我是否会一直传递一个接受 JQuery 元素的回调。)
将 JQuery 元素和 Ajax 响应作为回调参数传递的最佳方法是什么?
【问题讨论】:
标签: javascript jquery callback