JQuery keyup 事件在外部 js 文件中不起作用
好吧,这让我很困惑。 首先一些背景:
我创建了一个 html 文件(它实际上是一个 php 文件,但 php 只是用于表单验证)我决定创建一些 javascript 来进行一些即时验证,例如检查用户名是否已被使用。一切都工作得很好。然后我决定我可能会在网站的其他部分使用此代码,因此我开始摆弄包含代码的外部 js 文件。
在原始 html 代码中:
<script type="text/javascript">
$(document).ready(function(){
if($("#userN").val() == ""){
$("#testUserName").html("");
}else{
ajax_search();
}
$("#userN").focusout(function(){
ajax_search();
});
$("#userN").keyup(function(){
ajax_search();
});
} );
</script>
ajax_search() 只是向 php 文件发送请求,以测试 userN 输入字段中文本的数据。
所以我将其和 ajax_search() 函数复制到 test.js 文件中,确保首先调用 jquery.js 文件,然后使用该代码调用 test.js 文件。
奇怪的是 $(document).ready 运行
$(document).ready(function(){
alert("test1");
if($("#userN").val() == ""){
$("#testUserName").html("");
}else{
ajax_search();
}
$("#userN").focusout(function(){
ajax_search();
});
$("#userN").keyup(function(){
ajax_search();
});
alert("test2");
} );
该代码将显示一个带有 test1 的弹出窗口,然后显示另一个带有 test2 的弹出窗口,但 focusout 或 keyup 不起作用。
直到我将所有代码放回到 html 文件中(减去 ajax_search 函数)之前,我所做的一切都不起作用。这就是我想要的,但我不明白为什么这些活动不起作用。我做错了什么吗?难道不能这样吗?
Ok this is just confusing me.
first some background:
I created a html file ( it was actually a php file but the php was just for form validation) I decided to create some javascript to do some validation on the fly like check if the username had already been used. All was working just fine. I then decided I might have a use for this code in other parts of the site so I just started messing around with external js files holding the code.
in the original html code:
<script type="text/javascript">
$(document).ready(function(){
if($("#userN").val() == ""){
$("#testUserName").html("");
}else{
ajax_search();
}
$("#userN").focusout(function(){
ajax_search();
});
$("#userN").keyup(function(){
ajax_search();
});
} );
</script>
ajax_search() just sends a request to a php file to test the data with text in the userN input field.
so I copied that and the ajax_search() function to a test.js file I made sure the jquery.js file was called first, then I called the test.js file with that code in it.
The strange thing is the $(document).ready runs
$(document).ready(function(){
alert("test1");
if($("#userN").val() == ""){
$("#testUserName").html("");
}else{
ajax_search();
}
$("#userN").focusout(function(){
ajax_search();
});
$("#userN").keyup(function(){
ajax_search();
});
alert("test2");
} );
That code will show a popup with test1 then another popup with test2 but the focusout or keyup would not work.
Nothing I did worked until I put all that code back in the html file minus the ajax_search function. Which is what I wanted but I don't understand why the events would not work. Was I doing something wrong? Can it not be done this way?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用
$("#userN").on('keyup', function(){ ajax_search(); });
Use
$("#userN").on('keyup', function(){ ajax_search(); });
代码对我来说似乎没问题,你的问题出在其他地方。尝试这个代码并让我们知道结果 -
Code seems OK to me, your problem laies somewhere else. Try this code and let us know the result -
我有同样的错误,问题是我在两个不同的文件中实现了 document.onkeyup 。解决办法:合并或者删除其中一个
I had the same error, the problem was that I had document.onkeyup implemented in 2 different files. solution: merge them or delete one of them