【问题标题】:How to add values inserted using jquery .load()如何添加使用 jquery .load() 插入的值
【发布时间】:2014-11-05 23:35:31
【问题描述】:

我有一些使用 jquery .load 插入的值。我想将这些值加在一起并将结果显示在 div (或 span 或其他)中。这是我用来插入值的代码(数字,但只是纯文本):

结束正文标签上方:我有:

$( "#result1" ).load( "mypage.php?f=15 .threadcount" );
$( "#result2" ).load( "mypage.php?f=16 .threadcount" );
$( "#result3" ).load( "mypage.php?f=25 .threadcount" );
$( "#result4" ).load( "mypage.php?f=18 .threadcount" );    

并在页面上方显示这些值:

<div id="result1"></div>, <div id="result2"></div>... etc.

将它们加在一起并显示我尝试使用this answer to "Add values together using jquery"的总数

但这给了我“$NaN”作为我的总价值。我假设是因为它在求和的同时加载该值?或者它无法将 frm .load 中的那段文本识别为数字?

我还尝试更改 this answer to "Dynamically adding the sum of field in Jquery" 答案中使用的代码,该代码在输入表单输入时添加值,但我也没有得到任何结果。

非常感谢您提供的任何建议。

【问题讨论】:

    标签: jquery load addition


    【解决方案1】:

    所有.load() 操作都是异步的。这意味着它们将在未来的某个时间完成。您将不得不等到它们全部完成,直到您可以正确计算您的总和。我将添加一些示例代码来说明如何做到这一点。

    var cnt = 0;
    function done() {
        --cnt;
        if (cnt <= 0) {
            // all requests done now
            // put code here to calculate your sum
        }
    }
    
    cnt = 4;
    $( "#result1" ).load( "mypage.php?f=15 .threadcount" , done);
    $( "#result2" ).load( "mypage.php?f=16 .threadcount" , done);
    $( "#result3" ).load( "mypage.php?f=25 .threadcount" , done);
    $( "#result4" ).load( "mypage.php?f=18 .threadcount" , done);
    

    或者,使用 jQuery 承诺的版本:

    function loadResult(target, query) {
        var def = $.Deferred();
        $("#result" + target).load("mypage.php?f=" + query + " .threadcount", function() {
            def.resolve();
        });
        return def.promise();
    }
    
    $.when(loadResult(1,15), loadResult(2,16), loadResult(3,25), loadResult(4,18)).then(function() {
        // calculate the result here
    });
    

    【讨论】:

    • 感谢您的回复。这两种方法我都试过了,但我在这方面的技能有限!我想我是带着你的第一个建议到达那里的,但我再次得到 $NaN 应该是总和的地方。这是我使用的代码(你的加上我计算总和的尝试)
    • (糟糕,忘记回车键发送评论)var cnt = 0; function done() { --cnt; if (cnt &lt;= 0) { var sum = 0; $('#result1,#result2,#result3,#result4').each(function(){ sum += parseFloat($(this).text().substr(1)); }); $('#total_result').text('$' + Math.round(sum * 100) / 100); } } cnt = 4; $( "#result1" ).load( "mypage.php?f=15 .threadcount" , done); $( "#result2" ).load( "mypage.php?f=16 .threadcount" , done); $( "#result3" ).load( "mypage.php?f=25 .threadcount" , done); $( "#result4" ).load( "mypage.php?f=18 .threadcount" , done);
    • 对不起,不知道我在这里尝试输入代码做错了什么!使用了反引号,但它没有很好地显示。仍然掌握这个系统的窍门。
    • @Jo_pinkish - 您不能将多行代码放入 cmets。它根本不可读。如果您需要共享一些多行代码,您可以使用编辑链接将其添加到答案的末尾(正确格式化为代码,并说明添加它的原因),然后在此处添加注释指向您希望看到的个人,将他们指向该编辑,以便他们注意到它。
    • @Jo_pinkish - 如果你得到NaN,那么很明显,你在求和时没有正确找到或解析数字。一些基本的调试是有序的。使用console.log() 检查中间值,看看你得到了什么中间值,试图弄清楚发生了什么。
    【解决方案2】:

    您可以使用延迟对象来完成此操作,如下所示:

    var 
    d1 = $.get( "mypage.php?f=15", function(data) { $("#result1").html( $(data).find(".threadcount") ); }),
    d2 = $.get( "mypage.php?f=16", function(data) { $("#result2").html( $(data).find(".threadcount") ); }),
    d3 = $.get( "mypage.php?f=25", function(data) { $("#result3").html( $(data).find(".threadcount") ); }),
    d4 = $.get( "mypage.php?f=18", function(data) { $("#result4").html( $(data).find(".threadcount") ); });
    
    $.when( d1, d2, d3, d4 ).then( function() {
        var sum = +$('#result1').text() + $('#result2').text() + $('#result3').text() + $('#result4').text();
    });
    

    【讨论】:

    • 我认为你的意思是你的变量是 d1、d2、d3、d4,而不是全部 d1。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-08-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-06
    相关资源
    最近更新 更多