gpt4 book ai didi

javascript - .data,信息以 NaN 的形式传递?

转载 作者:行者123 更新时间:2023-11-27 22:57:55 25 4
gpt4 key购买 nike

我正在尝试构建一个简单的页面,它将计算您的赌注并计算出您的返回,就像博彩店一样。

到目前为止,我通过在我的赔率中进行硬编码来使其工作。但正如您将在我的代码中看到的那样,我有机会通过警报,效果很好。我的问题是尝试将此变量合并到我的 html 中,我得到“NaN”的输出,但我不知道为什么!我的尝试已被注释掉,但希望这是一个简单的修复,但我只是看不到!

我的 HTML:

 <html>

<head>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous">
<title></title>
<script type = "text/javascript"
src = "http://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>

<script type="text/javascript" language="javascript">
$(document).ready(function() {

$.getJSON("http://localhost:8888/json.json", function(result){

$.each(result, function(i, field){
//create an element and append data attribute to the element - in this case field name
el = $("<div class='bet-block'>" + field.name + field.odds.numerator+"/" + field.odds.denominator + " </div>").data("name", field.name).data("odds", field.odds.numerator+"/" + field.odds.denominator);

//append element to DOM
$(".bets").append( el );
})
//define a click handler for all new elements to display data in a div
$(document).on( "click", ".bet-block", function() {
$(".bet-name").text( $( this ).data("name") );
$(".bet-odds").text( $( this ).data("odds") );
});
});


function calculate_odds() {
var winnings = 0,
betting_amount = $(".stake").val();

var current_odds = $('.bet-odds').text();


// THIS ECHOS CORRECT ODDS
alert(current_odds);

winnings = (2/1) * betting_amount;
// winnings = (current_odds) * betting_amount;

$('.js-winnings').text(winnings);
}

$(document).ready(function() {
$(".stake").keyup(function(){
calculate_odds();
});
});
});

</script>
</head>

<body>

<div class="container">
<div class="row">
<div class="col-md-8">
<div class="bets">
<h1>All Bets</h1>
</div>
</div>
<div class="col-md-4">
<div class="slip">
<h1>My Bet Slip</h1>
<div class="new-bet-slip">
<p><span class="bet-name"></span> to win @ <span class="bet-odds"></span></p>
<input class="stake" type="number" name="betting-amount" min="1" max="5">
<div class="bet-return">
<span>£</span><span class="js-winnings">0.00</span>
</div>
</div>
</div>
</div>
</div>
</div>


</body>

</html>

我的 JSON:

[{"name":"name1","odds":{"numerator":10,"denominator":1}},[{"name":"name2","odds":{"numerator":5,"denominator":2}}, [{"name":"name3","odds":{"numerator":2,"denominator":1}}, ]

提前致谢!

最佳答案

您输入的 JSON 数据格式不正确,请检查方括号并重试。这个网站对这个目的很有帮助:https://jsonformatter.curiousconcept.com/

关于javascript - .data,信息以 NaN 的形式传递?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37414067/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com