简体   繁体   English

jQuery返回[object Object]

[英]jquery returns [object Object]

I have a little question about an issue I'm dealing with quite a long time now: I have an HTML which displays a list of items, stored in h2db. 关于我现在已经处理很长时间的问题,我有一个小问题:我有一个HTML,其中显示了存储在h2db中的项目列表。 These items show up correctly. 这些项目正确显示。

<tr th:each="item : ${list}">
  <td th:text="${item.getActualcost()}" id="actualcost">Actual Cost</td>
</tr>

I now would like to add another column to this list, showing a progress bar. 我现在想在此列表中添加另一列,以显示进度条。

I added this code to the HTML-part: 我将此代码添加到HTML部分:

<div class="container">
  <div class="col-md-4">
    <div class="progress">
          <div id="progressPlaceholder"></div>
    </div>

The script-part looks like this: 脚本部分如下所示:

<script>
    var rank=$('actualcost'),place=4;
    var progress = '<div class="progress-bar" role="progressbar" aria-valuenow="'+rank+'" aria-valuemin="0" aria-valuemax="100" style="width: '+rank+'%;"><span class="show" id="totalUsers">Ranked '+place+' of '+rank+'</span></div>';
    $('#progressPlaceholder').empty().append(progress);
</script>

The problem is, that the progress bar is not beinge filled out correctly. 问题是,进度条没有正确填写。 So I tried to find out what value it gets by adding '+rank+'. 因此,我尝试通过添加“ + rank +”来找出其价值。 Unforntunately I get only [object Object] printed out. 不幸的是,我只打印出[object Object]。 Can someone help me please with that problem? 有人可以帮我解决这个问题吗? The value should be an int. 该值应为int。

Rank is an object for you. 排名是一个对象。

console.log(rank) 

should give you the object, then use the appropriate key. 应该给你对象,然后使用适当的密钥。 It could look like: 它可能看起来像:

rank['value']//just an example

When you get a an element with jQuery, you get a jQuery collection, which again is an object. 当您使用jQuery获得一个元素时,您将获得一个jQuery集合,它又是一个对象。

var rank = $('actualcost')

Note that the above would assume you have an element looking like <actualcost></actualcost> , that you probably don't have. 请注意,上面假设您有一个类似<actualcost></actualcost>的元素,您可能没有。

Even if the element isn't found, just calling $() returns an object. 即使找不到元素,只需调用$()返回一个对象。

An objects string representation is always [object Object] . 对象字符串表示始终是[object Object]

When you concatenate an object and a string, you get the objects string representation, which is ... wait for it ... always [object Object] 当您连接对象和字符串时,您将获得对象字符串表示形式,即...等待它...始终[object Object]

var str = 'something something' + $('actualcost');

// str === 'something something[object Object]'

This is exactly what happens in your code 这正是代码中发生的情况

var rank=$('actualcost'),place=4;
var progress = '<div class="progress-bar" role="progressbar" aria-valuenow="'+rank+' ....

You probably wanted 你可能想要

var rank = $('#actualcost').text() ,place=4;

depending on what the HTML actually looks like (I don't know Thymeleaf) 取决于HTML的实际外观(我不知道Thymeleaf)

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM