简体   繁体   English

为什么我的填充文本区域的功能只能在第一次使用?

[英]Why does my function, that fills a textarea, only work the first time?

Javascript: 使用Javascript:

function editQuestion() {
  $("body").on("click", ".edit_question", function(event) {
    $(this).closest("form").find("textarea").text("sample text");
    event.preventDefault();
  });
}

$(document).ready(function() {  
  editQuestion();
});

HTML: HTML:

<form>
  <textarea>ormar</textarea>
  <div class="actions">
    <a href="#" class="edit_question">Cancel</a>
  </div>
</form>

When firing off the function by clicking the .edit_question link, everything is working as expected the first time. 通过单击.edit_question链接.edit_question该功能时,所有功能.edit_question预期首次运行。

But clicking more times (after manually removing the textarea content) doesn't do anything, why is this? 但是单击更多次(在手动删除textarea内容之后)没有任何作用,这是为什么?

.text() only changes the initial value of the textarea, since the initial value is read from the element's content ( <textarea>Initial value</textarea> ). .text()只改变textarea的初始值,由于初始值是从元素的内容读取( <textarea>Initial value</textarea> )。

Once you modified the value, the current value is stored in the value property of the DOM element. 修改值后,当前值将存储在DOM元素的value属性中。 Changing the initial value after the textarea was modified doesn't have any effect. 修改文本区域后更改初始值没有任何效果。

That's why you should use .val() intead of .text() . 这就是为什么您应该在.text() .val()使用。

Updated Demo compare with Original Demo 更新的演示原始演示比较

Use .val() instead of .text() in case of inputs. 输入时,请使用.val()而不是.text()

Check this, 检查一下

$(document).ready(function () {
    $(document).on("click", ".edit_question", function (event) {

        $(this).closest("form").find("textarea").val('sample text');
        event.preventDefault();
    });
});

Note: The .text() method cannot be used on form inputs or scripts. 注意: .text()方法不能在表单输入或脚本上使用。

Try using the .val() method rather than .text() method for textarea 尝试将.val()方法而不是.text()方法用于textarea

From documentation page: 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. 若要设置或获取输入或textarea元素的文本值,请使用.val()方法。 To get the value of a script element, use the .html() method. 若要获取脚本元素的值,请使用.html()方法。

link: http://api.jquery.com/text/ 链接: http //api.jquery.com/text/

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

相关问题 为什么(更改)function 只有在我第一次离开时才有效? - Why does the (change) function work only if I leave for first time? 为什么我在 vue.config.js 中的chainWebpack 只在我修改后第一次生效? - Why does my chainWebpack in vue.config.js only work the first time after I change it? 为什么我的 javascript 删除卡片按钮只在第一次工作但在后续使用时发送类型错误? - why does my javascript remove card button only work the first time but send a type error on subsequent uses? 为什么我的功能只能在Firefox中使用? - Why does my function only work in Firefox? 为什么平滑scrollBy仅在第一次工作? - Why does smooth scrollBy only work first time? 为什么我的点击处理程序仅适用于第一个元素? - Why does my click handler only work for the first element? 为什么我的 onclick 功能只有在第一次点击后才能工作? - Why does my onclick funciton only work after the first click? 为什么我的for循环只能在第一个元素上工作? - why does my for loop only work on the first element? 为什么我的javascript函数仅返回数组的第一项? - Why does my javascript function returns only first item of an array ? 为什么我的js函数只能找到第一个图像? - Why does my js function only find the first image?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM