我想知道如何在这两个小脚本中使用toLocaleString()。我尝试了不同的方法,但似乎没有任何效果。谁能解释我在做什么错?
$(window).load(function(){
$('input[type="radio"]').change(function () {
var total_span = 0;
$('input[type="radio"]').each(function () {
total_span += this.checked && +this.value;
});
$({counter: + $('#total').text()}).animate({counter: total_span},{
duration: 1000,
easing: 'swing',
step: function () {
$('#total').text(Math.floor(this.counter));
$('#total-2').text(Math.floor(this.counter));
},
complete : function(){
$('#total').text(total_span.toLocaleString("sv-SE"));
$('#total-2').text(total_span.toLocaleString("sv-SE"));
}
});
});
});
和
$(document).ready(function () {
$(':radio').change(function (e) {
//clear the div
$('#final').html('');
//update the div
$(':radio:checked').each(function (ind, ele) {
$('#final').append('<div><h4>' + $(ele).attr("name") + ':</h4>' + $(ele).attr("id") + '<br />' + $(ele).val().toLocaleString("sv-SE") + ':-<br/></div>');
});
});
});
如果有人可以让我知道如何解决这个问题,我将不胜感激。
非常感谢!
答案 0 :(得分:0)
也许你是这个意思? toLocaleString似乎可以正常工作
$(function() {
$('input[type="radio"]').change(function() {
var total_span = 0;
$('#final').empty(); // same as .html("")
$('input[type="radio"]:checked').each(function() { // Only take the checked radios
var val = +this.value; // the + makes the value numeric if it is a number
val = isNaN(val) ? 0 : val; // check it IS a number
total_span += val; // and add it
$('#final').append('<div><h4>' + this.name + ':</h4>' + this.id + '<br />' + val.toLocaleString("sv-SE") + ':-<br/></div>');
});
$({
counter: +$('#total').text()
}).animate({
counter: total_span
}, {
duration: 1000,
easing: 'swing',
step: function() {
$('#total').text(Math.floor(this.counter));
$('#total-2').text(Math.floor(this.counter));
},
complete: function() {
$('#total').text(total_span.toLocaleString("sv-SE"));
$('#total-2').text(total_span.toLocaleString("sv-SE"));
}
});
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
100.01 <input type="radio" name="rad1" id="rad1" value="100.01" /><br />
200.02 <input type="radio" name="rad2" id="rad2" value="200.02" /><br />
300.03 <input type="radio" name="rad3" id="rad3" value="100.03" /><br />
<div id="final"></div>
Total <span id="total"></span>
Total-2 <span id="total-2"></span>