在 Jquery 中设置全局变量 onclick
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19021044/
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
Set Global Variable onclick in Jquery
提问by ak85
I want to set a global variable in Jquery so that when I click on a list item, the id of the clicked list item becomes the value of the variable. I have the below code which is also in this fiddle.
我想在 Jquery 中设置一个全局变量,这样当我点击一个列表项时,被点击的列表项的 id 成为变量的值。我有下面的代码,它也在这个小提琴中。
However the way I have it: At the moment when you click the list item,the value correctly gets put in the console.log.However afterthat when I click in the demo div,the variable is reset and is undefined.
然而,我的方式是:当您单击列表项时,该值会正确放入 console.log。但是之后,当我在演示 div 中单击时,该变量被重置且未定义。
How do I get the value of id when I click on demo,to be the value of id when I click on the list item?
我如何在单击演示时获取 id 的值,以成为单击列表项时的 id 值?
eg :
例如:
when I click on aaa,the value when I click on the demo box should be id-1
当我点击aaa时,我点击演示框时的值应该是id-1
when I click on bbb,the value when I click on the demo box should be id-2
当我点击bbb时,我点击演示框时的值应该是id-2
<div id="demo"></div>
<ul>
<li id="id-1">aaa</li>
<li id="id-2">bbb</li>
</ul>
<script>
var id;
jQuery(document).on("click", "li", function (event) {
var id =jQuery(this).attr('id') || '';
console.log(id);
$( "#demo" ).show();
});
$(function() {
$("#demo").click(function(e) {
console.log(id);
});
});
</script>
<style>
#demo {
border: 1px solid;
display:none;
height: 100px;
width: 100px;
}
</style>
回答by Arun P Johny
var id;
jQuery(document).on("click", "li", function (event) {
//should not not var here, using var here will declare the variable id as a local variable in the handler function scope
id = jQuery(this).attr('id') || '';
console.log(id);
$("#demo").show();
});
$(function () {
$("#demo").click(function (e) {
console.log(id);
});
});
回答by PSR
You are again declared id
inside the loop.
您再次id
在循环内声明 。
var id;
jQuery(document).on("click", "li", function (event) {
id =jQuery(this).attr('id') || '';
console.log(id);
$( "#demo" ).show();
});
change
改变
var id =jQuery(this).attr('id') || ''; to
id =jQuery(this).attr('id') || ''; inside the loop.
回答by Dipak Ingole
This is because you re declare var id
这是因为你重新声明 var id
var id; // GLOBAL
jQuery(document).on("click", "li", function (event) {
var id =jQuery(this).attr('id') || ''; // LOCAL TO THIS BLOCK
so just use Global variable ,
所以只需使用全局变量,
var id; // GLOBAL
jQuery(document).on("click", "li", function (event) {
id =jQuery(this).attr('id') || ''; // Use GLOBAL id here
回答by Dev
Demo http://jsfiddle.net/devmgs/LbZe6/1/
演示http://jsfiddle.net/devmgs/LbZe6/1/
dont use var inside as it will make it local variable
不要在内部使用 var ,因为它会使其成为局部变量
var id;
jQuery(document).on("click", "li", function (event) {
id =jQuery(this).attr('id') || '';
console.log(id);
$( "#demo" ).show();
});
$(function() {
$("#demo").click(function(e) {
console.log(id);
});
});