gpt4 book ai didi

jQuery 如何?将附加参数传递给 $.ajax 调用的成功回调?

转载 作者:行者123 更新时间:2023-12-03 21:49:30 25 4
gpt4 key购买 nike

我试图将额外的参数传递回我为成功的 ajax 调用创建的成功回调方法,但似乎是徒劳的。一点背景。我有一个页面,其中包含许多动态创建的文本框/选择框对。每对都有一个动态分配的唯一名称,例如 name="unique-pair-1_txt-url"和 name="unique-pair-1_selectBox",那么第二对具有相同的名称,但前缀不同。

为了重用代码,我精心设计了回调来获取数据和对选择框的引用。但是,当回调被触发时,对选择框的引用将返回为“未定义”。我读过here这应该是可行的。我什至尝试利用“上下文”选项,但仍然一无所获。这是我尝试使用的脚本 block :

<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(以及我 future 的引用):

<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>

最佳答案

这就是我所做的,而且效果也很好:

$.ajax('URL', {
myCustomControl: selectBox,
myCustomVariable: 'teste',
data:
{
myData: 1
},
success: function (data, textStats, jqXHR) {
myFunction(data, textStats, jqXHR, this.myCustomControl, this.myCustomVariable);
}
});

您可以将控件和变量添加到 ajax 调用的参数中。

关于jQuery 如何?将附加参数传递给 $.ajax 调用的成功回调?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2602981/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com