php 如何更改 jQuery 中的标签文本?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10458564/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 22:12:52  来源:igfitidea点击:

How can I change the label text in jQuery?

phpjqueryajaxonchange

提问by user1362002

I have a textbox and a label. If the user wants to change the label text, he/she would have to enter the text in the textbox. How can I update the label text every time a user makes an entry in the textbox on change?

我有一个文本框和一个标签。如果用户想要更改标签文本,他/她必须在文本框中输入文本。每次用户在文本框中输入更改内容时,如何更新标签文本?

I know it is possible using AJAX with jQuery and PHP. Is there an easier way? Please let me know. Here is my code and a link: http://jsfiddle.net/uQ54g/1/

我知道可以将 AJAX 与 jQuery 和 PHP 结合使用。有更容易的方法吗?请告诉我。这是我的代码和链接:http: //jsfiddle.net/uQ54g/1/

$('#change1').change(function(){
    var l = $(this).val();
    $('label').replaceWith(l);


});

采纳答案by Josh Mein

First of all, you have to use .textor .htmlto set the text within a label in jquery. Your current code will actually remove the label element and replace with just text. Therefore, it will no longer work if you try to edit the value again. You also may want to add a class or an id to your label because most likely you will have more than one label on a page.

首先,您必须在 jquery 中使用.text.html设置标签内的文本。您当前的代码实际上将删除标签元素并仅替换为文本。因此,如果您再次尝试编辑该值,它将不再起作用。您可能还想在标签中添加一个类或一个 id,因为很可能在一个页面上会有多个标签。

HTML:

HTML:

<input type="text" id="change1"/><br/>
<label id="test">Hello</label>

Javascript:

Javascript:

$('#change1').change(function(){
    var l = $(this).val();
    $('#test').text(l);
    // OR $('#test').html(l);
});

If you are wanting to change the label after every character entered you will need to use keyupinstead of change.

如果您想在输入每个字符后更改标签,则需要使用keyup代替change

Here is an updated jsfiddle?

这是更新的jsfiddle

回答by Rafay

The code in you fiddle is working fine as i mentioned in the comments, or you can use keyuplike

正如我在评论中提到的,你小提琴中的代码工作正常,或者你可以使用keyup

$('#change1').keyup(function(){
var l = $(this).val();
$('label').text(l);
//alert($('#change1').val())
});

http://jsfiddle.net/uQ54g/4/

http://jsfiddle.net/uQ54g/4/

also note that when you use replaceWithit will replace the matched element, in your case it labelafter the change is triggered the <label>hello</label>will be replace with Changed Textso if you do $("label")again it will return you undefined, see it for yourself

还请注意,当您使用replaceWith它时将替换匹配的元素,在您的情况下,label在触发更改<label>hello</label>后将替换为,Changed Text因此如果您$("label")再次这样做,它将返回您未定义,自行查看

回答by ChrisGP

try this:

尝试这个:

$('#change1').keyup(function(){
l=$(this).val()
$('label').html(l)
})

回答by arun

use this with jquery library

将此与 jquery 库一起使用

$('#change1').on('keyup',function(){ //Change1 is id of textbox
   $('#label1').html($(this).val()) //label1 is the id of the label
});
$('#change1').blur(function() { //if we copy paste some text into the textbox it will also work if use this blur and trigger function
   $(this).trigger('keyup');
});

回答by Surya Pratap

How about this

这个怎么样

$('#change1').change(function(item){
     $('label').text(item.target.value);
});