热门关键字:
jquery > jquery教程 > jquery教程 > 如何在jQuery中获取textarea的值

如何在jQuery中获取textarea的值

364
作者:管理员
发布时间:2021/2/8 14:22:39
评论数:0
转载请自觉注明原文:http://www.jq-school.com/Show.aspx?id=3961
答:使用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
友荐云推荐