如何使用jQuery获取文本框值

吕峰
2023-12-01

要获取文本框值,可以使用jQuery val()函数。

例如,

  1. $('input:textbox')。val()-获取文本框值。
  2. $('input:textbox')。val(“新短信”)–设置文本框值。

文字框示例

<html>
<head>
<title>jQuery get textbox value example</title>
 
<script type="text/javascript" src="jquery-1.3.2.min.js"></script>

</head>

<body>

<h1>jQuery get textbox value example</h1>

<h2>TextBox value : <label id="msg"></label></h2>

<div style="padding:16px;">
	TextBox : <input type="textbox" value="Type something"></input>
</div>
	
<button id="Get">Get TextBox Value</button> 
<button id="Set">Set To "ABC"</button> 
<button id="Reset">Reset It</button>

<script type="text/javascript">
    $("button:#Get").click(function () {
		
	$('#msg').html($('input:textbox').val());
		
    });
	
    $("button:#Reset").click(function () {
		
	$('#msg').html("");
	$('input:textbox').val("");
		
    });
	
    $("button:#Set").click(function () {
		
	$('input:textbox').val("ABC");
	$('#msg').html($('input:textbox').val());
		
    });
	
</script>

</body>
</html>

翻译自: https://mkyong.com/jquery/how-to-get-textbox-value-with-jquery/

 类似资料: