jQuery插件问题-使用JSON数据填充选择选项 [英] jQuery plugin question - populate select options with JSON data
本文介绍了jQuery插件问题-使用JSON数据填充选择选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试用来自Web服务的json数据填充选择.我收到错误消息:对象不支持此属性或方法."当我这样做$(this).html(options.join(''));
有什么想法我做错了吗?
I'm trying to fill selects with json data from a web service. I'm getting error 'Object doesn't support this property or method.' when I do this $(this).html(options.join(''));
Any ideas what I'm doing wrong?
;(function($) {
$.fillSelect = {};
$.fn.fillSelect = function(url, map) {
var jsonpUrl = url + "?callback=?";
$.getJSON(jsonpUrl, function(d) {
var options = [];
var txt = map[0];
var val = map[1];
options.push('<option>--Select--</option>');
$.each(d, function(index, item) {
options.push('<option value="' + item[val] + '">' + item[txt] + '</option>');
});
$(this).html(options.join(''));
//getting error Object doesn't support this property or method
};
};
})(jQuery);
推荐答案
问题出在变量this
上.在您使用的上下文中,this
可能是指jQuery对象本身(即不是结果集).试试这个:
The problem is the variable this
. In the context you're using, this
is probably referring to the jQuery object itself (that is, not the result set). Try this:
$.fn.fillSelect = function (url, map) {
var $t = this; // (this) is the jQuery result set
$.getJSON( ... blah blah,
$t.html(options.join(''));
)
}
这篇关于jQuery插件问题-使用JSON数据填充选择选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文