我似乎徒劳地尝试能够将其他参数传递回我为成功的ajax调用创建的成功回调方法。一点背景。我有一个包含许多动态创建的文本框/选择框对的页面。每对具有动态分配的唯一名称,例如 name="unique-pair-1_txt-url" 和 name="unique-pair-1_selectBox" 然后第二对具有相同但前缀不同。
为了重用代码,我设计了回调来获取数据和对选择框的引用。但是,当触发回调时,对选择框的引用返回为“未定义”。我在这里读到它应该是可行的。我什至尝试过利用“上下文”选项,但仍然没有。这是我尝试使用的脚本块:
<script type="text/javascript" language="javascript">
$j = jQuery.noConflict();
function getImages(urlValue, selectBox) {
$j.ajax({
type: "GET",
url: $j(urlValue).val(),
dataType: "jsonp",
context: selectBox,
success:function(data){
loadImagesInSelect(data)
} ,
error:function (xhr, ajaxOptions, thrownError) {
alert(xhr.status);
alert(thrownError);
}
});
}
function loadImagesInSelect(data) {
var select = $j(this);
select.empty();
$j(data).each(function() {
var theValue = $j(this)[0]["@value"];
var theId = $j(this)[0]["@name"];
select.append("<option value='" + theId + "'>" + theValue + "</option>");
});
select.children(":first").attr("selected", true);
}
</script>
从我读过的内容来看,我觉得我很接近,但我不能把手指放在缺失的链接上。请以典型的忍者隐秘方式提供帮助。TIA
****更新****尼克是一个真正的忍者。他们应该为此发明一个新徽章!他在下面的回答可以解决问题。正如他提到的那样,它是 1.4 特定的,但我可以忍受。这是我的最终代码,适用于任何 Ninjas 培训(以及我未来的参考):
<script type="text/javascript" language="javascript">
$j = jQuery.noConflict();
function getImages(urlValue, selectBox) {
$j.ajax({
type: "GET",
url: urlValue+ '?callback=?',
dataType: "jsonp",
context: selectBox,
success: jQuery.proxy(function (data) {
var select = $j(this);
select.empty();
$j(data).each(function() {
var theValue = $j(this)[0]["@value"];
var theId = $j(this)[0]["@name"];
select.append("<option value='" + theId + "'>" + theValue + "</option>");
});
select.children(":first").attr("selected", true);
}, selectBox),
error:function (xhr, ajaxOptions, thrownError) {
alert(xhr.status);
alert(thrownError);
}
});
}
</script>