无法将数组值传递给单击函数
我计划在页面上显示一些图像,当用户按下向上按钮时,将显示一些相关的新图像。我通过更改已有图像 html 标签的“src”属性来实现这一点。
我有像 [["a","b"],["c","d"]]
这样的 json 数据,它给出了像 a.jpg、b.jpeg 这样的图像名称 等。
这是我无法将数组值传递给 jquery click 对象的问题。我的javascript功能如下:
var a; // a global variable holding the json array [["a","b"],["c","d"]]
function rileri(){ //img next
var ix=parseInt($("#up").attr('rel'));
ix=(ix+1)%a.length;
for(var i=0;i<2;i+=1){
$("#k"+i).attr("src","img/m/t/"+a[ix][i]+".jpg");
$("#k"+i).click(function() { rgetir(a[ix][i]);}); //problem is here!!
}
$("#up").attr('rel',ix); // i keep index data of appearing img on "rel"
}
function rgetir(n){ //img down ajax
$("#brc").load("data/rgetir.php", {'name':n});
}
我想知道如何绑定点击事件以及是否有更好的解决方案?
html 就是这样(开始时没有“src”,可以吗?):
<img id="k0"/><img id="k1"/>
<input id="up" rel="0" type="button" onclick="rileri()" value="Next">
是的,主要问题是将多维数组值传递给函数:(
i plan to show some images on the page and when the user press up button some related new images will be shown. I have been achieving that by changing the "src" attribute already existing image html tag.
i have json data like [["a","b"],["c","d"]]
which gives the name of images like a.jpg, b.jpeg etc.
Here is the problem i can not pass the array value to jquery click object. my javascript functions as below:
var a; // a global variable holding the json array [["a","b"],["c","d"]]
function rileri(){ //img next
var ix=parseInt($("#up").attr('rel'));
ix=(ix+1)%a.length;
for(var i=0;i<2;i+=1){
$("#k"+i).attr("src","img/m/t/"+a[ix][i]+".jpg");
$("#k"+i).click(function() { rgetir(a[ix][i]);}); //problem is here!!
}
$("#up").attr('rel',ix); // i keep index data of appearing img on "rel"
}
function rgetir(n){ //img down ajax
$("#brc").load("data/rgetir.php", {'name':n});
}
I wonder how can i bind click event and if there is any better solutions ?
html is like that (no "src" at start, is it ok ?):
<img id="k0"/><img id="k1"/>
<input id="up" rel="0" type="button" onclick="rileri()" value="Next">
Yeap the main problem is passing the multidimensional array value to a function :(
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这个问题与“多维数组”无关。这是因为您在分配的点击值中使用了
i
,并且i
随着循环的每次迭代而变化。每个分配的点击值都包含对i
的引用,因此当rileri
完成时,每个点击值都指向相同的值。您需要中断引用,通常通过将i
传递给函数并在其中绑定点击来完成。使用函数来中断引用的方式有很多种,但由于您使用的是 jQuery 并迭代数组,因此我们将使用
$.each
:(以下内容未经测试,但应作为例子)
The problem has nothing to do with "multidimensional arrays." It is that you are using
i
inside the assigned click value, andi
changes with every iteration of the loop. Each assigned click value holds a reference toi
, so whenrileri
is finished each points to the same value. You need to break the reference, usually done by passingi
to a function and binding the click in there.There are many flavors of using a function to break a reference, but since you're using jQuery and iterating an array, we'll use
$.each
:(what follows is untested but should serve as an example)
这是一个简单的小提琴,显示您的问题
http://jsfiddle.net/MHJx6/
问题是您的 ix和 i 变量是闭包,因此在事件运行时它们具有错误的值,如本示例中所示。
我尝试编写一些代码来完成我认为您在下面尝试做的事情。如果我知道你想做什么(用例),那就更容易了。也许这就是你想要的?
Here is a simple fiddle that shows your problem
http://jsfiddle.net/MHJx6/
The problem is your ix and i variables are closures, so at the time the event runs they have the wrong values as you can see in this example.
I tried to write some code that will do what I think you are trying to do below. It would be easier if I knew what you were trying to do (use case). Maybe this is what you want?