gpt4 book ai didi

javascript - 无法在按键时获取动态添加的文本区域值

转载 作者:行者123 更新时间:2023-12-02 14:25:15 25 4
gpt4 key购买 nike

一直工作得很好,刚才我为其他函数重构了代码,现在出现了这个愚蠢的问题,我不知道为什么会发生这种情况。

文本区域的 jquery 标记

 +'<div class="ckit-container__ft" data-ckit-footer>'
+'<form action="" class="ckit-composer-form">'
+'<div class="ckit-composer">'
+'<textarea data-ckit-composer-textarea placeholder="Add your reply" autocomplete="off" name="message" class="form-control ckit-composer__textarea"></textarea>'
+'</div>'
+'</form>'
+'</div>

绑定(bind)按键事件

jQuery(function($){
jQuery('body').bind('keypress','[data-ckit-composer-textarea]',function(event)
{
console.log(jQuery('[data-ckit-composer-textarea]').val());//returns empty
console.log(jQuery(this).val());//returns empty string
});
});

由于 HTML 标记存在于 $(function(){..}) 之外,我什至尝试克隆模板并找到要替换的相关属性,但没有成功。

var fullChatView = '<div class="ckit-container__ft" data-ckit-footer>'
+'<form action="" class="ckit-composer-form">'
+'<div class="ckit-composer">'
+'<textarea data-ckit-composer-textarea placeholder="Add your reply" autocomplete="off" name="message" class="form-control ckit-composer__textarea"></textarea>'
+'</div>'
+'</form>'
+'</div>';

var test = $(fullchatView).clone();//ReferenceError: fullchatView is not defined
var val = test.find('[data-ckit-composer-textarea]').val();
console.log(val);

最佳答案

jQuery('body').on('keypress','[data-ckit-composer-textarea]',function(event) { });

Documentation

As of jQuery 1.7, the .on() method is the preferred method for attaching event handlers to a document. For earlier versions, the .bind() method is used for attaching an event handler directly to elements.

关于javascript - 无法在按键时获取动态添加的文本区域值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38319268/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com