无法将数组值传递给单击函数

发布于 2024-12-17 01:16:47 字数 1009 浏览 0 评论 0原文

我计划在页面上显示一些图像,当用户按下向上按钮时,将显示一些相关的新图像。我通过更改已有图像 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

栩栩如生 2024-12-24 01:16:47

这个问题与“多维数组”无关。这是因为您在分配的点击值中使用了i,并且i随着循环的每次迭代而变化。每个分配的点击值都包含对 i 的引用,因此当 rileri 完成时,每个点击值都指向相同的值。您需要中断引用,通常通过将 i 传递给函数并在其中绑定点击来完成。

使用函数来中断引用的方式有很多种,但由于您使用的是 jQuery 并迭代数组,因此我们将使用 $.each:(

以下内容未经测试,但应作为例子)

function rileri(){
  var ix=parseInt($("#up").attr('rel'),10);
  ix=(ix+1)%a.length; 

  $.each(a[ix], function (i) {
    var img_name = this;

    $("#k"+i)
      .attr("src","img/m/t/"+img_name+".jpg")
      .click(function () {
        rgetir(img_name);
      });

      if (i >= 2)
      {
        return false;
      }
  });

  $("#up").attr('rel',ix);
}

The problem has nothing to do with "multidimensional arrays." It is that you are using i inside the assigned click value, and i changes with every iteration of the loop. Each assigned click value holds a reference to i, so when rileri is finished each points to the same value. You need to break the reference, usually done by passing i 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)

function rileri(){
  var ix=parseInt($("#up").attr('rel'),10);
  ix=(ix+1)%a.length; 

  $.each(a[ix], function (i) {
    var img_name = this;

    $("#k"+i)
      .attr("src","img/m/t/"+img_name+".jpg")
      .click(function () {
        rgetir(img_name);
      });

      if (i >= 2)
      {
        return false;
      }
  });

  $("#up").attr('rel',ix);
}
鱼窥荷 2024-12-24 01:16:47

这是一个简单的小提琴,显示您的问题

http://jsfiddle.net/MHJx6/

问题是您的 ix和 i 变量是闭包,因此在事件运行时它们具有错误的值,如本示例中所示。


我尝试编写一些代码来完成我认为您在下面尝试做的事情。如果我知道你想做什么(用例),那就更容易了。也许这就是你想要的?

var a; // a global variable holding the json array [["a","b"],["c","d"]]

function rileri(){ //img next
   $("#k0").click(function() { handleClick(0); });
   $("#k1").click(function() { handleClick(1); });
}


function handleClick(index) {
  var ix=parseInt($("#up").attr('rel'));
  ix=(ix+1)%a.length; 

  $("#k"+index).attr("src","img/m/t/"+a[ix][index]+".jpg"); 

  $("#up").attr('rel',ix); 

}

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?

var a; // a global variable holding the json array [["a","b"],["c","d"]]

function rileri(){ //img next
   $("#k0").click(function() { handleClick(0); });
   $("#k1").click(function() { handleClick(1); });
}


function handleClick(index) {
  var ix=parseInt($("#up").attr('rel'));
  ix=(ix+1)%a.length; 

  $("#k"+index).attr("src","img/m/t/"+a[ix][index]+".jpg"); 

  $("#up").attr('rel',ix); 

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文