jQuery追加错误显示浮点数

时间:2014-05-14 06:14:44

标签: javascript jquery

自从我开始研究这个问题以来已经过了一个小时,我很难找到问题

这是我在循环中的代码

$.each(data,function(key,game){
    console.log(game.teams_odds[1].game_odds_value_modified);
    var home_odds = parseFloat(game.teams_odds[0].game_odds_value_modified).toFixed(2);
    var away_odds = parseFloat(game.teams_odds[1].game_odds_value_modified).toFixed(2);
    console.log(away_odds);
    $("#oddsTable").append('<tr>' +
                                '<td>' +
                                    '<span class="red">Game ID : '+game.game_id+' </span>' +
                                        '<br>'+game.game_start_time+'<br>' +
                                '</td>' +
                                '<td>' +
                                    '<span class="teamName">'+game.teams_odds[0].team_name_eng+'</span>' +
                                    '<span class="teamName">'+game.teams_odds[1].team_name_eng+'</span>' +
                                '</td>' +
                                '<td>' +
                                    '<span style="display: block">' +
                                        '<a href="{if $isDotNet == 0}/{$kioskUrl}/single-pop/{$dataHolders.team_home_game_details}/bettype/{$betType}/baseball/{else}/pop_bet.php?gdid={$dataHolders.team_home_game_details}{/if}" class="teamOdds{if $dataHolder.blink_it == 1 && $dataHolder.blink_it_game_details_id == $dataHolder.game_details_id_home} blinkMe{/if}" style="font-size: 15px; font-weight: bold;">' +
                                            +home_odds+
                                        '</a>' +
                                    '</span>' +
                                    '<span style="display: block">' +
                                        '<a href="{if $isDotNet == 0}/{$kioskUrl}/single-pop/{$dataHolders.team_away_game_details}/bettype/{$betType}/baseball/{else}/pop_bet.php?gdid={$dataHolders.team_away_game_details}{/if}" class="teamOdds{if $dataHolder.blink_it == 1 && $dataHolder.blink_it_game_details_id == $dataHolder.game_details_id_away} blinkMe{/if}" style="font-size: 15px; font-weight: bold;">' +
                                            +away_odds+
                                        '</a>' +
                                    '</span>' +
                                    '<br>'+game.game_start_time+'<br>' +
                                '</td>' +
                           '</tr>'
                           );
});

当我尝试使用console.log()alert()来显示正确的值时,但是当我使用append看到我渲染的html时,数字缺少数字

1.60 变为1.6

console.log(away_odds) //1.60
alert(away_odds) //1.60

我想要的是显示1.60,但它会一直显示1.6

2 个答案:

答案 0 :(得分:3)

您已经连接了上一行:

'<a href="{if $isDotNet == 0} ... style="font-size: 15px; font-weight: bold;">' +
    +away_odds+ //                                                    extra + --^
'</a>' +

第二行的+前导Unary Plus operator实际上是{{3}},它会再次将away_odds转换为数字。此外,home_odds也会因同一原因转换为数字。

答案 1 :(得分:0)

toFixed(2)只应在输出它的代码部分中使用。

在这种情况下,

var home_odds = parseFloat(game.teams_odds[0].game_odds_value_modified);
var away_odds = parseFloat(game.teams_odds[1].game_odds_value_modified);

在打印变量的地方使用

home_odds.toFixed(2)

我测试了它并且工作正常