Javascript jQuery追加onclick函数

Javascript jQuery追加onclick函数,javascript,jquery,Javascript,Jquery,我已经在这个问题上纠缠了一整天了!基本上,我有一个数组: var category = { id_1:[ {id:"1_1", img:"img/prodotti-categorie/test.jpeg", titolo:"Name", prezzo:"8,50", link:""}, {id:"1_2", img:"img/prodotti-categorie/test.jpeg", titolo:"Name", prezzo:"7,00", l

我已经在这个问题上纠缠了一整天了!基本上,我有一个数组:

var category = {
    id_1:[
          {id:"1_1", img:"img/prodotti-categorie/test.jpeg", titolo:"Name", prezzo:"8,50", link:""},
          {id:"1_2", img:"img/prodotti-categorie/test.jpeg", titolo:"Name", prezzo:"7,00", link:""},
          {id:"1_3", img:"img/prodotti-categorie/test.jpeg", titolo:"Name", prezzo:"7,00", link:""},
          {id:"1_4", img:"img/prodotti-categorie/test.jpeg", titolo:"Name", prezzo:"19,00", link:""},
          {id:"1_5", img:"img/prodotti-categorie/test.jpeg", titolo:"Name", prezzo:"10,00", link:""},
          {id:"1_6", img:"img/prodotti-categorie/test.jpeg", titolo:"Name", prezzo:"8,50", link:""},
          {id:"1_7", img:"img/prodotti-categorie/test.jpeg", titolo:"Name", prezzo:"12,00", link:""},
          {id:"1_8", img:"img/prodotti-categorie/test.jpeg", titolo:"Name", prezzo:"7.50", link:""}],
    id_2:[...
然后我得到如下值:

for(var i=0, l=Object.keys(category["id_"+(cat_id)]).length; i<l; i++) {
        var img = category["id_"+(cat_id)][i]["img"];
        var title = category["id_"+(cat_id)][i]["titolo"];
        var price = category["id_"+(cat_id)][i]["prezzo"];

        $('#content').append('<div class="category-list item-'+ cat_id + '_' + i +'"><img src="' + img + '"><p>' + title +'</p><p>€' + price + '</p></div>');

       //From here begins my problem:
       //When i search fot the class added before and try to append or do anything else, variable i is always 8!
        $('.item-' + cat_id + '_' + i).click(function(){
             app.renderPageProductView(cat_id, i);
        });
    };

for(var i=0,l=Object.keys(category[“id”+(cat\u id)]).length;i在所有这些之后,您不能只做:

$(".item-1_1").off('click').on('click', function () {
   app.renderPageProductView(1,1);
});

让我们让这对你来说容易些

它们都使用相同的类:
.category list
,因此将该类别添加为数据元素(
数据cat='foo'
数据id='bar'

然后你可以做:

$(document).on('click','.category-list', function(){
     var data = $(this).data();

     app.renderPageProductView(data.cat, data.id);
});
您可以执行以下操作来设置所有设置:

for(id in category){
   for(var i = 0; i < category[id].length; ++i){
        var img = category[id][i]["img"];
        var title = category[id][i]["titolo"];
        var price = category[id][i]["prezzo"];

        $('#content').append('<div class="category-list" data-id="'+ i +'" data-cat="'+ id +'"><img src="' + img + '"><p>' + title +'</p><p>€' + price + '</p></div>');
   }
}
for(类别中的id){
对于(变量i=0;i<类别[id]。长度;++i){
var img=类别[id][i][“img”];
变量标题=类别[id][i][“标题”];
var价格=类别[id][i][“prezzo”];
$(“#内容”)。追加(“”+标题+”

€“+价格+”

”); } }
尝试将索引存储到数据属性中。循环完成后会执行单击,因此I将是最大值

请尝试以下代码:

   $('#content').append('<div data-index="' + i + '" class="category-list item-'+ cat_id + '_' + i +'"><img src="' + img + '"><p>' + title +'</p><p>€' + price + '</p></div>');

   //From here begins my problem:
   //When i search fot the class added before and try to append or do anything else, variable i is always 8!
    $('.category-list').click(function(){
         app.renderPageProductView(cat_id, parseInt($(this).attr('data-index'), 10));
    });
$(“#content”)。追加(“”+标题+”

€“+价格+”

”); //从这里开始我的问题: //当我搜索之前添加的类并尝试追加或执行任何其他操作时,变量i始终为8! $('.category list')。单击(函数(){ app.renderPageProductView(cat_id,parseInt($(this.attr('data-index'),10)); });
这看起来太复杂了……哈哈,谢谢你的帮助!$('.item-'+cat\u id+'\u'+i)需要被调用来为每个divwhere应用添加它。renderPageProductView(cat\u id,i);并得到i=8,而不是1 hanks@ExplosionPills dunno我怎么错过了
上的
,起初我以为有一些新的jQuery构造我不知道。为什么要使用attr来获取数据属性?使用
数据(…)
!我知道点击是在循环完成后执行的,但我不知道如何存储I。你的回答解决了我的问题,我理解。我知道。Mersi batke!