简体   繁体   English

为什么我使用jQuery获取[object object]?

[英]Why i'm getting [object object] using jQuery?

I have a function there i want to try sum two numbers using variable like given below, but i'm getting [object object] instead of total of sum. 我那里有一个函数,我想尝试使用如下所示的变量求和两个数字,但是我正在获取[object object]而不是总和。

 var convenienceCharge = 100; var totalPgCost = 500; var subTotalPgCost = 0; function totalPgCostFunction() { subTotalPgCost += $('.subTotalPgCost').text(convenienceCharge + totalPgCost + 18); alert(subTotalPgCost); } 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <button onclick="totalPgCostFunction();">Click</button> 

.text will return a object. .text将返回一个对象。 that is why you are seeing [object object] 这就是为什么您看到[object object]

You need to do it like this. 您需要这样做。

 var convenienceCharge = 100; var totalPgCost = 500; var subTotalPgCost = 0; function totalPgCostFunction() { subTotalPgCost += convenienceCharge + totalPgCost + 18; $('.subTotalPgCost').text(subTotalPgCost); alert(subTotalPgCost); } 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <button onclick="totalPgCostFunction();">Click</button> <div class="subTotalPgCost"></div> 

The issue is because when you call the setter of text() it returns a jQuery object, hence the output you see. 问题是因为当您调用text()的setter时,它将返回一个jQuery对象,因此您会看到输出。

To make this work you need to split the calculation and text() logic, like this: 为此,您需要拆分计算和text()逻辑,如下所示:

 var convenienceCharge = 100; var totalPgCost = 500; var subTotalPgCost = 0; $(function() { $('button').click(function() { subTotalPgCost += convenienceCharge + totalPgCost + 18; $('.subTotalPgCost').text(subTotalPgCost); }); }); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <button>Click</button> <div class="subTotalPgCost"></div> 

Also note the use of an unobtrusive event handler in this example instead of the outdated on* attribute. 还要注意,在此示例中使用了不显眼的事件处理程序,而不是过时的on*属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我使用布尔对象变得不确定? - Why i'm getting undefined using Boolean object? 我使用jquery错误选择了对象 - I'm selecting object incorrectly using jquery 为什么在进行发布请求时我得到空 object - Why I'm getting empty object when doing post request 我得到 [object Object] 而不是输出 - I'm getting [object Object] instead of output 为什么我使用 JQuery 得到的只是文本而不是 HTML? - Why i'm getting just text instead of HTML using JQuery? I'm using AJAX to post an object from javascript to php within the same page and I'm getting the entire html page instead of the object - I'm using AJAX to post an object from javascript to php within the same page and I'm getting the entire html page instead of the object 我收到jQuery [object Object]错误 - I am getting jQuery [object Object] error 为什么我得到未定义的对象,以及如何使用NodeJ解析json数据? - why I'm getting undefined object and How do I parse json data with NodeJs? 重构jQuery代码时,为什么会得到[object Object]而不是预期的HTML? - Why am I getting [object Object] instead of my expected HTML when refactoring jQuery code? 我需要将一个对象序列化为JSON。 我正在使用jQuery。 有没有“标准”的方式来做到这一点? - I need to serialize an object to JSON. I'm using jQuery. Is there a “standard” way to do this?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM