获取jquery文本表单的输入事件

编写一个示例代码,在jquery的“on”方法中使用“touchmove”获取文本表单的输入事件。

获取文本表单的输入事件

要获取文本表单的输入事件,请在“on”方法中指定“input”。

$('指定元素').on('touchmove', function() {
    // 处理
})

获取jquery文本表单的输入事件  第1张

以下是在指定元素“id="txt"”中输入字符时统计并显示字符数的示例代码。

<!DOCTYPE html>
<html>

<head>
	<meta charset="utf-8">
	<title>获取jquery文本表单的输入事件</title>
	<link href="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css" rel="stylesheet">
	<script src="http://libs.baidu.com/jquery/2.1.4/jquery.min.js"></script>

</head>

<script>

$(function () {

  $('#txt').on('input', function () {

    $('#result').text( `已输入字符:${$(this).val().length}` )
  })

})

</script>

<body>

	<div class="container text-center" style="margin-top:200px">
		<h2 id="result"></h2>
		<form>
			<div class="form-group">
				<input type="text" class="form-control" id="txt">
			</div>
		</form>
	</div>
	
</body>
</html>

执行结果

获取jquery文本表单的输入事件  第2张

也可以使用箭头函数编写如下。

$(() => {

  $('#txt').on('input', () => {

    $('#result').text( `已输入字符:${$(this).val().length}` )
  })

})


本文来源:词雅网

本文地址:https://www.ciyawang.com/jquery-form-touchmove.html

本文使用「 署名-非商业性使用-相同方式共享 4.0 国际 (CC BY-NC-SA 4.0) 」许可协议授权,转载或使用请署名并注明出处。

相关推荐