te')); return $arr; } /* 遍历用户所有主题 * @param $uid 用户ID * @param int $page 页数 * @param int $pagesize 每页记录条数 * @param bool $desc 排序方式 TRUE降序 FALSE升序 * @param string $key 返回的数组用那一列的值作为 key * @param array $col 查询哪些列 */ function thread_tid_find_by_uid($uid, $page = 1, $pagesize = 1000, $desc = TRUE, $key = 'tid', $col = array()) { if (empty($uid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('uid' => $uid), array('tid' => $orderby), $page, $pagesize, $key, $col); return $arr; } // 遍历栏目下tid 支持数组 $fid = array(1,2,3) function thread_tid_find_by_fid($fid, $page = 1, $pagesize = 1000, $desc = TRUE) { if (empty($fid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('fid' => $fid), array('tid' => $orderby), $page, $pagesize, 'tid', array('tid', 'verify_date')); return $arr; } function thread_tid_delete($tid) { if (empty($tid)) return FALSE; $r = thread_tid__delete(array('tid' => $tid)); return $r; } function thread_tid_count() { $n = thread_tid__count(); return $n; } // 统计用户主题数 大数量下严谨使用非主键统计 function thread_uid_count($uid) { $n = thread_tid__count(array('uid' => $uid)); return $n; } // 统计栏目主题数 大数量下严谨使用非主键统计 function thread_fid_count($fid) { $n = thread_tid__count(array('fid' => $fid)); return $n; } ?>javascript - value of HTML element is undefined but it is set - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - value of HTML element is undefined but it is set - Stack Overflow

programmeradmin3浏览0评论

I want to set checkbox after the page has renderet to the matching state of the backend.

I implemented this function for that purpose what does the job so far

Updated to user-suggestion It doesn't matter if cbs[i].dataset.thvalue or cbs[i].dataset.value

 function updateCheckboxes () {

  let activeCheckbox = '<input type="checkbox" onclick="handleClickOnReduce()" checked>'
  let inactiveCheckbox = '<input type="checkbox" onclick="handleClickOnReduce()">'

  let cbs = document.getElementsByTagName('td')
  for (let i = 0; i < cbs.length; i++) {
    if (cbs[i].id === 'reduceCheckbox' || cbs[i].id === 'slCheckbox') {
      if (cbs[i].dataset.thvalue === 'true') {
        cbs[i].innerHTML = activeCheckbox
      }
      else {
        cbs[i].innerHTML = inactiveCheckbox
      }
    }
  }
}

HTML part, using Thymeleaf

    <td id="reduceCheckbox" data-th-value="${car.isReduced()}"></td>
    <td id="slCheckbox" data-th-value="${car.isSl()}"></td>

But the browser claims by printing out the value, that it is undefined, even if the value is set as you can see in the picture live from the browser.

Due to the docu my syntax should be correct?

.asp

Any suggestion?

Ty

I want to set checkbox after the page has renderet to the matching state of the backend.

I implemented this function for that purpose what does the job so far

Updated to user-suggestion It doesn't matter if cbs[i].dataset.thvalue or cbs[i].dataset.value

 function updateCheckboxes () {

  let activeCheckbox = '<input type="checkbox" onclick="handleClickOnReduce()" checked>'
  let inactiveCheckbox = '<input type="checkbox" onclick="handleClickOnReduce()">'

  let cbs = document.getElementsByTagName('td')
  for (let i = 0; i < cbs.length; i++) {
    if (cbs[i].id === 'reduceCheckbox' || cbs[i].id === 'slCheckbox') {
      if (cbs[i].dataset.thvalue === 'true') {
        cbs[i].innerHTML = activeCheckbox
      }
      else {
        cbs[i].innerHTML = inactiveCheckbox
      }
    }
  }
}

HTML part, using Thymeleaf

    <td id="reduceCheckbox" data-th-value="${car.isReduced()}"></td>
    <td id="slCheckbox" data-th-value="${car.isSl()}"></td>

But the browser claims by printing out the value, that it is undefined, even if the value is set as you can see in the picture live from the browser.

Due to the docu my syntax should be correct?

https://www.w3schools./jsref/prop_option_value.asp

Any suggestion?

Ty

Share Improve this question edited Aug 2, 2018 at 20:05 Anna Klein asked Jul 25, 2018 at 10:59 Anna KleinAnna Klein 2,1714 gold badges32 silver badges64 bronze badges 1
  • Why not this way? stackoverflow./a/29925235/5138198 – Alex Nikulin Commented Aug 8, 2018 at 11:22
Add a ment  | 

3 Answers 3

Reset to default 11

You HTML is invalid.

<td> elements do not have value attributes. (The link you reference is talking about <option>, not <td>).

Your browser is not mapping the JS value property onto your invented attribute.

If you want to add arbitrary data to an element, use a data-* attribute and read it using the dataset API.

It looks like my misunderstanding of the Thymeleaf syntax with regards to th-data led to that issue.

This is the fix:

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

Access via

 console.log(cbs[i].dataset.value)

value is not valid attributes in td tag. you can use abbr attributes

<td id="reduceCheckbox" abbr="true"> <input type="checkbox" onclick="handleClickOnReduce()"> </td>

then you can get it

console.log(cbs[i].abbr)

Note: The abbr attribute is not supported in HTML5.

For HTML5 you can use headers attributes

<td id="reduceCheckbox" headers="true">
    <input type="checkbox" onclick="handleClickOnReduce()">
</td>

then you can get it

console.log(cbs[i].headers)
发布评论

评论列表(0)

  1. 暂无评论