jquery触发change事件
jquery触发change事件,在本教程中,我们将讨论change()方法,以及它在监听输入字段和下拉菜单上的更改时非常有用。
语法:
$(selector).change( HANDLER )
HANDLER(EVENT OBJECT) –触发事件时要执行的函数。
代码
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js">
</script>
<script>
$(document).ready(function(){
$("#name_field").change(function(){
alert("Value has been changed to: "+$(this).val());
});
});
</script>
</head>
<body>
<input type="text" id="name_field"> Write Something here.
</body>
</html>
在上面的示例中,每次在文本字段中输入内容时,都会显示一条警告消息,说明已进行了更改。原因是输入框默认值为空。当您在字段中输入内容时,这会触发change()方法,因为原始值为null,并被更改为您输入的文本。
change() 方法不仅限于文本字段,还适用于下拉菜单。有关更多信息,请参见下面的示例。
代码
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js">
</script>
<script>
$(document).ready(function(){
$("#dropdown").change(function(){
alert("Value has been changed to: "+$(this).val());
});
});
</script>
</head>
<body>
<select id="dropdown">
<option></option>
<option>1</option>
<option>2</option>
<option>3</option>
<option>4</option>
</select>
Select from the options using the dropdown.
</body>
</html>
以上就是jquery触发change事件,的全部内容了,希望对你有帮助。
常见问题FAQ
- 程序仅供学习研究,请勿用于非法用途,不得违反国家法律,否则后果自负,一切法律责任与本站无关。
- 请仔细阅读以上条款再购买,拍下即代表同意条款并遵守约定,谢谢大家支持理解!