簡體   English   中英

Jquery 按回車鍵獲取 textarea 值

[英]Jquery get textarea value on pressing enter

我試圖在按下回車鍵時獲取文本區域的值,但獲取值未定義。

jQuery(".comment-form .comments-text").on('keypress', function (e) {
    if(e.which === 13 && !e.shiftKey) {
        e.preventDefault();
        
        var text = jQuery(this).value;
        console.log(e);
    }
});

on keypress 工作正常,但無法獲取我輸入的文本。

此代碼應該可以工作:

HTML

<form class="comment-form">
  <textarea class="comments-text"></textarea>
</form>

JQuery

$(".comment-form .comments-text").on('keypress', function(e) {
  if (e.which === 13 && !e.shiftKey) {
    e.preventDefault();

    var text = e.target.value;
    console.log('text', text);
  }
});

您可以使用val();獲取textArea的值; jquery中的方法

let value = $("textArea").val();
alert(value);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM