要使用jQuery获取文本框的值,请遵循以下步骤:
<head>
部分添加以下代码来实现:
<input>
元素)并为其分配一个唯一的ID。例如:
function getTextBoxValue() {
var textBoxValue = $("#myTextBox").val();
console.log("文本框的值是:" + textBoxValue);
}
getTextBoxValue()
函数。例如:
完整的HTML和JavaScript代码示例:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>jQuery获取文本框的值示例</title>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script>
function getTextBoxValue() {
var textBoxValue = $("#myTextBox").val();
console.log("文本框的值是:" + textBoxValue);
}
</script>
</head>
<body>
<input type="text" id="myTextBox" />
<button onclick="getTextBoxValue()">获取文本框的值</button>
</body>
</html>
当用户在文本框中输入文本并单击按钮时,控制台将显示文本框的值。
云+社区沙龙online第5期[架构演进]
云+社区技术沙龙[第21期]
北极星训练营
北极星训练营
云+社区技术沙龙[第10期]
北极星训练营
腾讯云GAME-TECH沙龙
高校公开课
领取专属 10元无门槛券
手把手带您无忧上云