答:使用jQueryval()方法
您可以使用jQueryval()方法获取或设置<textarea>元素的值。确保删除任何结尾和前导空格,否则可能会导致意外结果。
以下示例将从文本区域获取值,并在按钮不相等""(即不为空)时单击该按钮,在警报对话框中向您显示。
例试试这个代码»
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Get Value from Textarea in jQuery</title>
<script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
<script>
$(document).ready(function(){
$("button").click(function(){
var comment = $.trim($("#comment").val());
if(comment != ""){
// Show alert dialog if value is not blank
alert(comment);
}
});
});
</script>
</head>
<body>
<textarea id="comment" rows="5" cols="50"></textarea>
<p><button type="button">Get Value</button></p>
<p><strong>Note:</strong> Type something in the textarea and click the button to see the result.</p>
</body>
</html>
如果您觉得本文的内容对您的学习有所帮助:
关键字:
jQuery