未定义 Jquery 函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8799733/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Jquery function not defined
提问by Shakir
I have a problem with loading this code. I am sure its something to do with noConflict but i cant seem to get it right.
我在加载此代码时遇到问题。我确定它与 noConflict 有关系,但我似乎无法正确理解。
$(document).ready(
function spouseName() {
if ($('#maritalstatus').val() == 'married') {
$('#spousename').attr("disabled", false);
} else {
$('#spousename').val('');
$('#spousename').attr("disabled", true);
}
}
);
by the way, it works on IE but not FF
顺便说一句,它适用于 IE 但不适用于 FF
采纳答案by Shakir
thanks for the info and answers, it seems this thread helped
感谢您的信息和答案,看来这个线程有帮助
firefox does not recognize the function name when it inside the jquery document.ready function. what i did was wrap it inside although it seems unconventional. I just removed the document ready and it works perfectly. seems chrome and FF dont recognize function names within this?
当它在 jquery document.ready 函数中时,firefox 无法识别函数名称。我所做的是把它包在里面,虽然它看起来非常规。我刚刚删除了准备好的文档,它运行良好。似乎 chrome 和 FF 无法识别其中的函数名称?
function spouseName() {
if ($('#maritalstatus').val() == 'married') {
$('#spousename').attr("disabled", false);
}
else {
$('#spousename').val('');
$('#spousename').attr("disabled", true);
}
}
}
回答by Bipin Chandra Tripathi
The "jQuery not defined" error happens if you have not included jQuery or may imported beneath your JavaScript.
如果您没有包含 jQuery 或可能在您的 JavaScript 下导入,则会发生“jQuery 未定义”错误。
It should be like this:
应该是这样的:
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"> </script>
<script type="text/javascript" src="myjsfile.js"> </script>
myjsfile.js
should be like this:
myjsfile.js
应该是这样的:
$(document).ready(function(){
every function inside this
});
回答by Andreas Wong
You forgot the function() after ready()
你在 ready() 之后忘记了 function()
should be:
应该:
$(document).ready(function() {
function spouseName() { // your code }
function anotherFunction() { }
});
回答by Darin Dimitrov
Your question is not very clear. Are you using $.noConflict
? If so there is an example in the documentation:
你的问题不是很清楚。你在用$.noConflict
吗?如果是这样,文档中有一个示例:
<script type="text/javascript" src="other_lib.js"></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script type="text/javascript">
$.noConflict();
jQuery(document).ready(function($) {
if ($('#maritalstatus').val() == 'married') {
$('#spousename').attr("disabled", false);
}
else {
$('#spousename').val('');
$('#spousename').attr("disabled", true);
}
});
// Code that uses other library's $ can follow here.
</script>
But in all cases make sure that you have properly referenced the jquery script itself before attempting to use it.
但在所有情况下,请确保在尝试使用 jquery 脚本之前正确引用了它。
回答by QMaster
In some cases, jQuery can't find a function because of scopeissues. You could define the function as below and important subject is to set the function scope to global:
在某些情况下,由于范围问题,jQuery 找不到函数。您可以如下定义函数,重要的主题是将函数作用域设置为全局:
jQuery(document).ready(function($) {
function sample() { console.log('blah blah'); }
window.sample = sample; //export function sample to the globals.
})
or shorter form as below:
或更短的形式如下:
(function($) {
function sample() { console.log('blah blah'); }
window.sample = sample; //export function sample to the globals.
})(jQuery)
Hope this help.
希望这有帮助。