Jquery change() 不起作用

Jquery change() not working

本文关键字:不起作用 change Jquery      更新时间:2023-09-26

大家好,我是jquery的新手。我遇到了更改函数的问题,我查询

<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js">
</script>
<script>
  $("input").change(function(){
    alert("The text has been changed.");
  });
</script>
</head>
<body>
<input type="text">
<p>Write something in the input field, and then press enter or click outside the field.</p>
</body>
</html>

当我在文本框外按下它时,我没有收到警报框。请帮助我解决此问题:D

您正在尝试在将元素添加到 dom 之前将更改处理程序添加到元素。您可以使用 dom ready 回调来延迟脚本执行,直到所有元素都加载到 dom

jQuery(function($){
  $("input").change(function(){
    alert("The text has been changed.");
  });
})

演示:小提琴

你也可以试试这个:

    $(function() {
     $("input").change(function(){
            alert("The text has been changed.");
    });
 })

你也可以试试这个:

$(document).ready(function(){
$("input").change(function(){
    alert("The text has been changed.");
  });
});

把你的代码放在:

$(function(){
    // Your code
});

脚本将等待 DOM 准备好启动。

相关文章: