HTML数据集为空

时间:2018-08-03 16:59:48

标签: javascript thymeleaf

我正在使用HTML,JS,并通过Thymleaf将值传递给td元素,如下所示。

  <tr th:each="car : ${cars}">
        <td id="reduceCheckbox" data-th-value="${car.isReduced()}"></td>
    </tr>

这在HTML元素中可用

enter image description here

但是当我尝试从数据集中检索值时,集合中没有任何东西(长度= 0)

我还发现了一些建议使用getAttribute来代替,但这并不能带来价值。

  let cbs = document.getElementsByTagName('td')
  for (let i = 0; i < cbs.length; i++) {
    if (cbs[i].id === 'reduceCheckbox' || cbs[i].id === 'slCheckbox') {
      console.log(cbs[i].dataset) /*length = 0*/
      console.log(cbs[i].dataset.value) /*undefined*/
      console.log(cbs[i].dataset.thvalue)  /*undefined*/
     /* console.log(cbs[i].dataset.th-value) */ /*value is not defined*/
      console.log(cbs[i].getAttribute("data-th-value")) /*null*/
      console.log(cbs[i].getAttribute("data-value")) /*null*/
      if (cbs[i].dataset.thvalue === 'true') {
        cbs[i].innerHTML = activeCheckbox
      }
      else {
        cbs[i].innerHTML = inactiveCheckbox
      }
    }
  }

任何想法我该如何继续进行if / else块获取相关数据?

我已经在考虑将此td元素形成一个字符串,只是检查它是否包含value=truevalue=false,但对我来说似乎有点脏。

0 个答案:

没有答案
相关问题