Javascript jQuery 全局变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8389062/
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 Global Variable
提问by jQuerybeast
Can someone explain me why this doesn't work:
有人可以解释我为什么这不起作用:
$(function() {
var damg = $(".hidden").val();
$("button").click(function() {
alert(damg);
});
});
jsFiddle: http://jsfiddle.net/raFnT/
jsFiddle:http: //jsfiddle.net/raFnT/
Is it good to use global variables? I've also read that it is a slower option than typing it every time?
使用全局变量好不好?我还读到它比每次都输入要慢?
To explain in detail:
详细解释一下:
I have this:
我有这个:
$("button").click(function() {
alert(damg);
});
and the damg is a value of the input: var damg = $(".hidden").val();
damg 是输入的值: var damg = $(".hidden").val();
When you type something in the input and THEN press the button, alert the value of that input.
当您在 input 中键入内容然后按下按钮时,警告该 input 的值。
I could use
我可以用
$("button").click(function() {
var damg = $(".hidden").val();
alert(damg);
});
but in one point that I will have 100 functions I will end up like this:
但在某一点上,我将拥有 100 个函数,我最终会像这样:
$("button1").click(function() {
var damg = $(".hidden").val();
alert(damg);
});
$("button2").click(function() {
var damg = $(".hidden").val();
alert(damg);
});
$("button3").click(function() {
var damg = $(".hidden").val();
alert(damg);
});
$("button4").click(function() {
var damg = $(".hidden").val();
alert(damg);
});
$("button5").click(function() {
var damg = $(".hidden").val();
alert(damg);
});
I want to set a global variable so that on every function I don't need to call the function again.
我想设置一个全局变量,以便在每个函数上我都不需要再次调用该函数。
Something like this:
像这样的东西:
var damg = $(".hidden").val();
$("button1").click(function() {
alert(damg);
});
$("button2").click(function() {
alert(damg);
});
$("button3").click(function() {
alert(damg);
});
$("button").click(function() {
alert(damg);
});
$("button4").click(function() {
alert(damg);
});
$("button5").click(function() {
alert(damg);
});
回答by BNL
You have it backwards, a global variable has to be written OUTSIDE of a function.
反过来说,全局变量必须写在函数的外部。
Your code doesn't work because .hidden doesn't have a value.
您的代码不起作用,因为 .hidden 没有价值。
It is bad practice to use global variables unless you really need them to be global. I don't know what you mean by 'typing it every time.'
除非您确实需要全局变量,否则使用全局变量是不好的做法。我不知道你所说的“每次都打字”是什么意思。
Also, you are not reloading the value in the click handler.
此外,您不会重新加载单击处理程序中的值。
$(function() {
$("button").click(function() {
var damg = $(".hidden").val();
alert(damg);
});
});
In the interest of having a good answer, what the OP wants is the blur
function.
为了有一个好的答案,OP 想要的是blur
函数。
$(function() {
var damg;
$(".hidden").blur(function () {
damg = $(".hidden").val();
});
$("button").click(function() {
alert(damg);
});
});
回答by Abe Miessler
Your example is working. The issue is that you are setting damg
on document ready. On document ready your text box has no value in it. If you assign a default value it will alert that value on button click. You can see this in action in this fiddle: http://jsfiddle.net/raFnT/1/
你的例子正在起作用。问题是您正在设置damg
文档准备就绪。在文档准备好后,您的文本框没有任何价值。如果您分配一个默认值,它会在按钮单击时提醒该值。你可以在这个小提琴中看到这个:http: //jsfiddle.net/raFnT/1/
If you would like to change the value of damg
on every button click change your code to this:
如果您想更改damg
每个按钮上的值,请单击将代码更改为:
var damg;
$("button").click(function() {
damg = $(".hidden").val();
alert(damg);
});
回答by Darek Rossman
The damg variable is being set immediately when the function first executes - you need to set that var in the click function itself so that it grabs the value from the field at that time.
damg 变量在函数第一次执行时被立即设置 - 您需要在 click 函数本身中设置该变量,以便它当时从字段中获取值。
Not sure why this was down voted - this is what you have asked for:
不知道为什么这被否决了 - 这就是你所要求的:
$(function() {
var damg = $(".hidden").val();
$("button").click(function() {
damg = $(".hidden").val();
alert(damg);
});
});
Or even more simply:
或者更简单:
$(function() {
$("button").click(function() {
alert( $(".hidden").val(); );
});
});
回答by Clinton Green
Just adding my two cents, here's the same solution but using a change function to monitor for changes.
只需添加我的两分钱,这是相同的解决方案,但使用更改功能来监视更改。
JS
JS
var damg;
$('#contact-name').on('change', function(){
damg = $(".hidden").val();
});
回答by Chris
That's not a global variable. That's a var scoped to the anonymous function you pass to jQuery's ready handler.
那不是全局变量。这是一个作用域范围为您传递给 jQuery 的就绪处理程序的匿名函数的 var。
The reason that doesn't work (and by not work, I assume you mean it alerts an empty string) is because damg
is assigned when the DOM loads. At that time, your text box doesn't have a value. So it actually is working. If you want the text box's value whenever you click the button, you need to get it's value inside the click handler.
不工作的原因(不工作,我假设你的意思是它提醒一个空字符串)是因为damg
在 DOM 加载时被分配。那时,您的文本框没有值。所以它实际上是有效的。如果您想要在单击按钮时获取文本框的值,则需要在单击处理程序中获取它的值。
You can cache the DOM lookup by doing something like this though:
您可以通过执行以下操作来缓存 DOM 查找:
$(function() {
var damg = $(".hidden");
$("#one").click(function() {
alert("button one: " + damg.val());
});
$("#two").click(function() {
alert("button two: " + damg.val());
});
});
<input type="text" class="hidden" placeholder="Type text here">
<button id="one">Click me(1)!</button>
<button id="two">Click me(2)!</button>