简体   繁体   English

如何使用 jquery 更改输入中的值?

[英]how do you change the value in the input using jquery?

I have an input with a value that is still dummy, when you click the pencil button the value can be changed in accordance with the input, how do I do that?我有一个值仍然是虚拟的输入,当您单击铅笔按钮时,可以根据输入更改值,我该怎么做? and when entered new values are automatically saved并在输入新值时自动保存

this is my html (click pencil and input)这是我的 html(点击铅笔并输入)

<span id="changeTitle" style="cursor:pointer;">
  <i class="fa fa-pencil"></i>&nbsp;&nbsp;
</span>
<input type="text" name="title" value="Love at The First Sight" id="sectionTitle" class="input-title">

and this is my jquery这是我的 jquery

$('#changeTitle').on('click', function(){
   var newText = $("input[name='title']").val();
   $('#sectionTitle').text(newText);
});

You can use .val() method instead of .text() :您可以使用.val()方法代替.text()

$('#changeTitle').on('click', function(){
   var newText = $("input[name='title']").val();
   $('#sectionTitle').val(newText);
});

Here, sectionTitle is actually an input textbox and as mentioned in the docs :在这里, sectionTitle实际上是一个input文本框,如文档中所述:

The .text() method cannot be used on form inputs or scripts. .text()方法不能用于表单输入或脚本。 To set or get the text value of input or textarea elements, use the .val() method.要设置或获取inputtextarea元素的文本值,请使用.val()方法。

So, instead of using $('#sectionTitle').text(newText);因此,不要使用$('#sectionTitle').text(newText); you can use:您可以使用:

$('#sectionTitle').val(newText);

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

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