我如何使用th:data-url

问题描述 投票:0回答:2

我需要从以下位置获取ID号:

<div class="social"  th:data-url="@{http://localhost:8080/poll?id=${poll.pollId}" data-title="">

但是这不起作用。我该怎么办?

javascript thymeleaf data-uri
2个回答
0
投票

希望这会有所帮助。

$(document).ready(function(){
  //get value of 'this' element that was clicked
  var url = $(this).attr('data-url');
  // split string and get value of id
  var splitted = url.split("id=");

  console.log("Id:"+splitted[1]);
});

或绑定onclickonchange事件

<div class="social" th:data-url="@{http://localhost:8080/poll?id=${poll.pollId}" th:onclick="'getId(\''+ ${poll.pollId} +'\')'" data-title="">

并获取id值

<script type="text/javascript">
       function getId(id) {
           console.log(id);
       }
</script>

0
投票

所有自定义属性均使用th:attr属性定义。喜欢:

<div class="social" data-title=""
  th:attr="data-url=@{|/poll?id=${poll.pollId}|}" data-title="">

还用URL或@{}指定href将建立完整的URL。

© www.soinside.com 2019 - 2024. All rights reserved.