用JQuery设置标签文本 [英] Set Label Text with JQuery
本文介绍了用JQuery设置标签文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这应该是非常直接的,但是下面的代码不会做任何事情,只要改变下一个标签的文本。我曾尝试使用.text,.html等无济于事。这段代码有什么问题吗?
This should be quite straight forward, however the following code does not do anything as far as changing the next label's text. I have tried using .text, .html, and so on to no avail. Is there anything wrong with this code?
<script type="text/javascript">
$(document).ready(function()
{
$("input:checkbox").on("change", checkboxChange);
function checkboxChange()
{
$("#"+this.id).next("label").text("TESTTTT");
}
});
</script>
<td width="15%" align="center"><input type="checkbox" name="task1" id="task1"></td>
<td width="25%" align="center"><label for="task1"></label></td>
推荐答案
复选框位于 td
,所以需要先获得父项:
The checkbox is in a td
, so need to get the parent first:
$("input:checkbox").on("change", function() {
$(this).parent().next().find("label").text("TESTTTT");
});
或者,找到一个< id
(可能比反向遍历更高性能):
Alternatively, find a label which has a for
with the same id
(perhaps more performant than reverse traversal) :
$("input:checkbox").on("change", function() {
$("label[for='" + $(this).attr('id') + "']").text("TESTTTT");
});
或者,要简洁一点 this.id $ c $ ()
Or, to be more succinct just this.id
:
$("input:checkbox").on("change", function() {
$("label[for='" + this.id + "']").text("TESTTTT");
});
这篇关于用JQuery设置标签文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文