Ajax更改页面上的文本

时间:2014-12-17 11:04:56

标签: javascript jquery ajax

你好,我有一些像这样的HTML:

<div class="col-md-4" id="sk6x4">
  <a href="/razbornye/models6x4/sk6x4">
  <span class="sceneName">СК6x4</span> <span class="scenePrice">671 384p.</span></a>
</div>
<div class="col-md-4" id="sk6x4">
  <a href="/razbornye/models6x4/sk6x4">
  <span class="sceneName">СК6x4</span> <span class="scenePrice">671 384p.</span></a>
</div>
<div class="col-md-4" id="sk6x4">
  <a href="/razbornye/models6x4/sk6x4">
  <span class="sceneName">СК6x4</span> <span class="scenePrice">671 384p.</span></a>
</div>

还有一些JS:

jQuery(document).ready(function($) {
    var col = $('.preview').find('.col-md-4 .sceneName');
    var urls = [];
    var ids = [];
    col.each(function(i) {
        var yy = $(this).closest('.col-md-4').find('a').attr('href');
        var xx = $(this).closest('.col-md-4').attr('id');
        urls.push(yy);
        ids.push(xx);
    });
    var dataUrls = function() {
        $.each(urls, function (i, url) {
        $.ajax({
            url: url,
            type: 'POST',
            success: function (data) {
                var apronFactor = (($(data).find('.table.table-bordered tr:eq(6) td:eq(2)').text()).replace(/\s+/g, ''))*1;
                var apronPrice = ($(data).find('.table.table-bordered tr:eq(6) td:eq(3)').text()).replace(/\s+/g, '');
                var apronPriceMR = apronPrice.substring(0, apronPrice.length - 2);
                var apronPriceToNum = apronPriceMR*1;
                var apronTotalPrice = apronFactor * apronPriceToNum;
                /**/
                var plankFactor = (($(data).find('.table.table-bordered tr:eq(7) td:eq(2)').text()).replace(/\s+/g, ''))*1;
                var plankPrice = ($(data).find('.table.table-bordered tr:eq(7) td:eq(3)').text()).replace(/\s+/g, '');
                var plankPriceMR = plankPrice.substring(0, plankPrice.length - 2);
                var plankPriceToNum = plankPriceMR*1;
                var plankTotalPrice = plankFactor * plankPriceToNum;
                /**/
                var tentFactor = (($(data).find('.table.table-bordered tr:eq(8) td:eq(2)').text()).replace(/\s+/g, ''))*1;
                var tentPrice = ($(data).find('.table.table-bordered tr:eq(8) td:eq(3)').text()).replace(/\s+/g, '');
                var tentPriceMR = tentPrice.substring(0, tentPrice.length - 2);
                var tentPriceToNum = tentPriceMR*1;
                var tentTotalPrice = tentFactor * tentPriceToNum;
                /**/
                var totalOptionsPrice = apronTotalPrice + plankTotalPrice + tentTotalPrice;
                /**/
                var complexWithoutOptionsPrice = ($(data).find('#cel_1 > .value').text()).replace(/\s+/g, ''); //cWOP
                var cWOPMR = complexWithoutOptionsPrice.substring(0, complexWithoutOptionsPrice.length - 2);
                var cWOPToNum = cWOPMR * 1;
                /**/
                var newTotalPrice = cWOPToNum + totalOptionsPrice;
                var newTotalPriceToString = newTotalPrice.toString().replace(/\B(?=(\d{3})+(?!\d))/g, " ") + "р.";
                $(ids[i] + ' .scenePrice').text(newTotalPriceToString));
            }

        });
    });
    }

    dataUrls();
});

任务是:   从另一页获取价格并替换此页面上的价格 问题是    $(ids [i] +&#39; .scenePrice&#39;)。text(newTotalPriceToString)); 它不起作用。

2 个答案:

答案 0 :(得分:1)

替换此行:

$(ids[i] + ' .scenePrice').text(newTotalPriceToString));

使用:

$('#'+ids[i] + ' .scenePrice').text(newTotalPriceToString));

我猜你只是传递了id的字符串,而不是#和它一起传递。

答案 1 :(得分:-1)

尝试找出问题线无效的原因:

alert(ids[i] + ' .scenePrice'); //check whether your selector is correct
alert($(ids[i] + ' .scenePrice').length); //check whether jQuery matched element

但从你的代码看,你可能只是错过了#&#39;#&#39;在id。前面。