【问题标题】:How can I add the thousands commas to these values? [duplicate]如何将数千个逗号添加到这些值? [复制]
【发布时间】:2020-03-13 01:33:30
【问题描述】:

当我点击总计时,它会将投票列值相加,但它将千位逗号作为小数逗号。因此,我收到的不是 20,000 + 30,000 = 50,000,而是 20,000 + 30,000 = 50。如何解决这个问题? ajax调用函数有问题吗?

这是 html 文件:

 <!DOCTYPE html>

<html>

<head>
<meta charset="UTF-8">

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>

<script>
$(document).ready(function(){
$('#red').on('click', function() {
var colorId = $(this).attr("id");

$.ajax({"url": "tables.php", "data": {"color" : colorId},
success: function(response) {
$("#redr").html(response);
console.log(response);
}
});
});

$('#orange').on('click', function() {
var colorId = $(this).attr("id");

$.ajax({"url": "tables.php", "data": {"color" : colorId},
success: function(response) {
$("#oranger").html(response);
console.log(response);
}
});
});

$('#green').on('click', function() {
var colorId = $(this).attr("id");

$.ajax({"url": "tables.php", "data": {"color" : colorId},
success: function(response) {
$("#greenr").html(response);
console.log(response);
}
});
});

$('#indigo').on('click', function() {
var colorId = $(this).attr("id");

$.ajax({"url": "tables.php", "data": {"color" : colorId},
success: function(response) {
$("#indigor").html(response);
console.log(response);
}
});
});


$('#yellow').on('click', function() {
var colorId = $(this).attr("id");

$.ajax({"url": "tables.php", "data": {"color" : colorId},
success: function(response) {
$("#yellowr").html(response);
console.log(response);
}
});
});

$('#blue').on('click', function() {
var colorId = $(this).attr("id");

$.ajax({"url": "tables.php", "data": {"color" : colorId},
success: function(response) {
$("#bluer").html(response);
console.log(response);
}
});
});

$('#total').on('click', function() {
var sumofval = 0;
$(".val").each(function () {
if ($(this).html() !== '')
sumofval = sumofval + parseInt($(this).html());
$('#totalr').html(sumofval);
console.log(sumofval);
});
});


});
</script>

<title>Finals</title>

<style>
table, th {
border: 1px solid white;
padding: 30px;
}

th {
text-align: left;
color: white;
background-color: #007acc;
}

tr {
background-color: #b3ffb3;
}

#tcol {
border-collapse: collapse;
}

td {
border: 1px solid white;
text-align: left;
padding: 10px;
}

</style>
</head>

<body>

<h1 style="font-size:30px;">FINALS</h1>
<table id="tcol" style="width:60%;">
<thead>
<tr>
<th>Color</th>
<th>Votes</th>
</tr>
</thead>
<tr>
<td id="red"><a href="#">Red</a></td>
<td id="redr" class="val"></td>
</tr>
<tr>
<td id="orange"><a href="#">Orange</a></td>
<td id="oranger" class="val"></td>
</tr>
<tr>
<td id="yellow"><a href="#">Yellow</a></td>
<td id="yellowr" class="val"></td>
</tr>
<tr>
<td id="green"><a href="#">Green</a></td>
<td id="greenr" class="val"></td>
</tr>
<tr>
<td id="blue"><a href="#">Blue</a></td>
<td id="bluer" class="val"></td>
</tr>
<tr>
<td id="indigo"><a href="#">Indigo</a></td>
<td id="indigor" class="val"></td>
</tr>
<tr>
<td id="total" style="font-weight: bold;"><a 
href="#">TOTAL</a></td>
<td id="totalr" style="font-weight: bold;"></td>
</tr>
</table>


</body>
</html>

这是 php 文件:

<?php
$servername = "localhost";
$username = "root";
$password = "";
$db_name = "finals";

$conn = new mysqli($servername, $username, $password, $db_name);
if ($conn->connect_error) {
echo "Connection failed: " . $conn->connect_error;
exit ();
}

$co = $_REQUEST['color'];

$sql = "SELECT SUM(votes) AS sum FROM Votes WHERE color = '$co'";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
$row = $result->fetch_assoc();
echo number_format($row["sum"]);
}
else {
echo 0;
}

$conn->close();

?>

【问题讨论】:

  • “以及应该输出0的颜色,输出​​“NaN”和浏览器控制台”,这是合乎逻辑的,因为你试图将“No result found”变成一个数字。
  • El_Vanja。哇,你是对的,很简单。好的,这解决了一个问题。所以,当颜色不存在时,我得到带有逗号和 0 的值。但是当我点击 TOTAL 并求和时,假设:2,000 + 3,000,我得到 5 作为结果。我想获得5,000
  • 第二部分是here。每当某些事情无法按预期工作时,dump 值(在本例中为console.log 它们)。他们都是。这将表明parseInt 是罪魁祸首。
  • 您的数据库中有什么?

标签: php jquery mysql ajax


【解决方案1】:

您没有定义完整的参数使用 number_format($row["sum"],2,'.','');

【讨论】:

    猜你喜欢
    • 2010-12-07
    • 1970-01-01
    • 1970-01-01
    • 2020-05-28
    • 1970-01-01
    • 1970-01-01
    • 2010-09-11
    相关资源
    最近更新 更多