[英]get td jstl value by javascript
I want to get value of td and print it to textbox but td has jstl value so it get the first value in arraylist but I want to get the current value in td when user click it. 我想获得td的值并将其打印到文本框但是td具有jstl值,因此它获得arraylist中的第一个值但是我想在用户单击时获取td中的当前值。 Can anyone help me how can I do that? 任何人都可以帮助我,我该怎么做?
$(document).ready(function() {
$("div.contacts table td").live('click', function() {
document.getElementById("contactName").value=
document.getElementById("contactId").innerHTML;
});
});
input text and td 输入文字和td
<input type="text" id="contactName">
<td id="contactId">{{contact.email}}</td>
You have to take the HTML of the clicked element, which is this
: 你必须要点击的元素,这是的HTML this
:
$(document).ready(function() {
$("div.contacts table td").live('click', function() {
$("#contactName").val($(this).text());
});
});
Note that the live()
method is deprecated. 请注意,不推荐使用live()
方法。 You should use on
instead. 你应该使用on
而不是。
Demo, using on
: 演示,使用on
:
$(document).ready(function() { $("div.contacts").on('click', "table td", function() { $("#contactName").val($(this).text()); }); });
td { cursor: pointer; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input type="text" id="contactName"> <div class="contacts"> <table> <thead> <tr> <th>Name</th> </tr> </thead> <tbody> <tr> <td>Alice</td> </tr> <tr> <td>Bob</td> </tr> <tr> <td>Carol</td> </tr> </tbody> </table> </div>
You're using your code in opposite way: 你以相反的方式使用你的代码:
document.getElementById("contactName").value=
document.getElementById("contactId").innerHTML;
Should be: 应该:
document.getElementById("contactId").innerHTML=
document.getElementById("contactName").value;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.