使用div类编号创建帐户



我正在尝试创建一个帐户,例如,我正在尝试将税款添加到一个值中,该值将为$10,税款将为$5

但我不能得出这个总数:(

有人能帮帮我吗?

function displayTax() {

var tax = this.gsx$taxvalue.$t ;

$('.taxClass').html(tax);

var item = 10;
var total = item + tax;

$('.total-count').html(item);

$('.total-tax').html(total.toFixed(2));

}
$(document).ready(function() {

var sheetURL =
"https://spreadsheets.google.com/feeds/list/1j3ri1WPLuw6IXXKYqrqNkSe_vU__YEXjyUSusR6G7ss/1/public/values?alt=json";
$.getJSON(sheetURL, function(data) {
var entryData = data.feed.entry;
console.log(data);
jQuery.each(entryData, function() {


$("#valueTax").append(
' <div>' + this.gsx$taxvalue.$t + '</div>'
);
});
});
});

displayTax();
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>


<div class="total-count"></div>

<div>Value Tax:<span class="taxClass"></span> </div>
<div id="valueTax"></div>
<br>
<div>Total with tax: $<span class="total-tax"></span></div>

本质上,当一个变量不存在时,您试图访问它,它们在不同的作用域中。

我写了一个你如何解决这个问题的例子。

function displayTax(tax) {
const intTax = parseInt(tax, 10);
$('.taxClass').html(tax);
var item = 10;
var total = item + intTax;
$('.total-tax').html(total.toFixed(2));
}
function loadData() {
var sheetURL =
"https://spreadsheets.google.com/feeds/list/1j3ri1WPLuw6IXXKYqrqNkSe_vU__YEXjyUSusR6G7ss/1/public/values?alt=json";
$.getJSON(sheetURL, function(data) {
var entryData = data.feed.entry;
console.log(data);
if(entryData) {
// in case this array have more than one entry this will be overwritten 
entryData.forEach(item => {
displayTax(item['gsx$taxvalue']['$t']);  
})

}

});
}
$(document).ready(function() {
loadData()
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="total-count"></div>

<div>Value Tax: $<span class="taxClass"></span> </div>
<div id="valueTax"></div>
<br>
<div>Total with tax: $<span class="total-tax"></span></div>

您可以在displayTax()函数中使用call,将函数内的this设置为所需对象。

displayTax.call(yourTaxObject);

或者在您的情况下是

displayTax.call(this);

最新更新