jQuery如何?传递额外的参数成功回调$.ajax调用?

前端之家收集整理的这篇文章主要介绍了jQuery如何?传递额外的参数成功回调$.ajax调用?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我似乎试图通过附加的参数回到我为一个成功的ajax调用创建的成功回调方法。一点背景我有一个页面与一些动态创建的文本框/选择框对。每个对具有动态分配的唯一名称,例如name =“unique-pair-1_txt-url”和name =“unique-pair-1_selectBox”,则第二对具有相同但前缀不同。

为了重用代码,我已经制作了回调来获取数据并引用了选择框。但是当回调被触发时,对selectBox的引用返回为’undefined’。我读了here它应该是可行的。我甚至尝试利用“上下文”选项,但仍然没有。这是我试图使用的脚本块:

<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,url: urlValue+ '?callback=?',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),thrownError) {
            alert(xhr.status);
            alert(thrownError);
        }
    });
}
</script>

解决方法

更新:如果您使用jQuery 1.4,请稍等一下简化:
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)

猜你在找的jQuery相关文章