如何在jQuery中获取一个textarea的值
我们可以在jQuery中通过val()方法获得textarea的值。val()方法是用来获取元素的值,如textarea,input和select。这个方法简单地返回或设置所选元素的值属性,主要用于表单元素。当在空的集合上调用时,或者当输入没有给定时,它返回未定义或空白的输出。
<!DOCTYPE html>
<html>
<head>
<title>How to get the value of textArea</title>
<script src=
"https://code.jquery.com/jquery-1.12.4.min.js">
</script>
</head>
<body>
<center>
<h1 style="color: green;">GeeksforGeeks</h1>
<textarea id="txtArea"></textarea><br><br>
<button id="btn">Show Text</button>
</center>
</body>
<script>
(document).ready(function () {
//This function called when the button is clicked
("#btn").click(function () {
// val() method is used to get the values from
// textarea and stored in txt variable
var txt = $("#txtArea").val();
alert(txt);
});
});
</script>
</html>
输出:
jQuery是一个开源的JavaScript库,它简化了HTML/CSS文档之间的交互,它以其 “少写多做 “的理念而广为人知。