简体   繁体   English

如何在Jquery中更改标签文本

[英]How Can I change Label Text in Jquery

Hi I am trying to change label text in an input field; 嗨,我正在尝试在输入字段中更改标签文本;

var check =  $("#name_label").find(".nameText").text("Malcom"); 
alert("check" + check);

here is my HTML 这是我的HTML

<input type="checkbox" class="NameList" id="name_label">
<label for="name_label">
<div class="nameText">&nbsp;</div>
</label>

right now it return object instead of changing the text. 现在,它返回对象而不是更改文本。 Please help. 请帮忙。

更改您的代码

 $("[for='name_label']").find(".nameText").text("Malcom")

The label is the next element after input but not inside input 标签是输入之后的下一个元素,但不是输入内部

$("#name_label").next('label').find('.nameText').text("Malcom");

To then check it you have to get the text inside (and not by puttinf the text in, that will result in an html object) 要进行检查,您必须将文本放入内部(而不是通过将文本插入其中,这将导致html对象)

var check = $("#name_label").next('label').find('.nameText').text();
alert("check " + check);

See code example below 请参见下面的代码示例

 $("#name_label").next('label').find('.nameText').text("Malcom"); var check = $("#name_label").next('label').find('.nameText').text(); alert("check " + check); 
 <script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <input type="checkbox" class="NameList" id="name_label"> <label for="name_label"> <div class="nameText">&nbsp;</div> </label> 

选择标签,然后选择其内部div并更改其html,如下所示

$("label[for=name_label]").find(".nameText").html("Malcom");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM