无法在Ajax调用

时间:2017-09-01 12:47:40

标签: jquery html ajax

我已经在div上进行了一次AJAX调用,其中包含'mydiv'类,如下所示:

但是在成功部分,我无法找到父div。 HTML:

<div class="mydiv">
    <a href="/jewelry/asscher-cut-diamond-side-stone-ring-with-white-diamond-in-14k-white-gold/antique-scroll-ring/269p2m7s1c" target="_blank"><img alt="White Gold Asscher Cut Diamond Filigree Engagement Ring" class="img-responsive" height="220" src="http://3aef1d7506efae8a24d3-e7821b1789d66a252f67999ba68e5823.r99.cf2.rackcdn.com/asscher-cut-diamond-shank-wave-side-stone-engagement-ring-in-14K-white-gold-FDENS3543ASR-NL-WG.jpg" width="220" /> </a>
    <h4>
        <a href="http://www.fascinatingdiamonds.com/jewelry/asscher-cut-diamond-side-stone-ring-with-white-diamond-in-14k-white-gold/antique-scroll-ring/269p2m7s1c" target="_blank">White Gold Asscher Cut Diamond Filigree Engagement Ring</a></h4>
    <div class="boxprices1"></div>  
</div>

Javascript代码:

$('#Tab-block .mydiv').each(function(){
var prodinfo =$(this).find('a').attr("href");
//*** here read the value successfully ***

$.ajax({
    type: "POST",
    contentType: "application/json; charset=utf-8",
    url: "/Services/Service.asmx/GetProductPriceRating",
    dataType: "json",
    data: "{'customize_code':'" + prodinfo + "'}",
    success: function (data) {

        $(this).find(".boxprices1").append("<b>$ " + data.d + "</b>");
        //*** issue here: this append is not happening ***

    },
    error: function (err) {
        //  alert(err);
    }
});
})

如何在AJAX成功中找到父div?

3 个答案:

答案 0 :(得分:0)

this内的{p> ajax是指ajax调用

&#13;
&#13;
$('#Tab-block .mydiv').each(function(){
var that = this;
var prodinfo =$(this).find('a').attr("href");
//*** here read the value successfully ***

$.ajax({
    type: "POST",
    contentType: "application/json; charset=utf-8",
    url: "/Services/Service.asmx/GetProductPriceRating",
    dataType: "json",
    data: "{'customize_code':'" + prodinfo + "'}",
    success: function (data) {

    $(that).find(".boxprices1").append("<b>$ " + data.d + "</b>");
    //*** issue here: this append is not happening ***

    },
    error: function (err) {
        //  alert(err);
    }
});
})
&#13;
&#13;
&#13;

要么把这个变成一些变量&amp;用它。 我已使用that变量来存储this引用。

希望这会对你有所帮助。

答案 1 :(得分:0)

这是一个范围问题。在success函数中,this是您的ajax对象。只需找到对父div的新引用:

$.ajax({
    type: "POST",
    contentType: "application/json; charset=utf-8",
    url: "/Services/Service.asmx/GetProductPriceRating",
    dataType: "json",
    data: "{'customize_code':'" + prodinfo + "'}",
    success: function (data) {
        $('.mydiv').find(".boxprices1").append("<b>$ " + data.d + "</b>");
    },
    error: function (err) {
        //  alert(err);
    }
});

答案 2 :(得分:0)

这就是你的代码应该是这样的。 您可以将此值赋给变量并在AJAX中使用它。

var parent = $(this); //assign the value of this to a variable & use it inside AJAX
$.ajax({
    type: "POST",
    contentType: "application/json; charset=utf-8",
    url: "/Services/Service.asmx/GetProductPriceRating",
    dataType: "json",
    data: "{'customize_code':'" + prodinfo + "'}",
    success: function (data) {
        parent.find(".boxprices1").append("<b>$ " + data.d + "</b>");
        //*** here: Use that parent dic to append data ***    
    },
    error: function (err) {
        //  alert(err);
    }
  });
})