无法动态地为我的变量赋值

时间:2014-04-03 21:56:06

标签: javascript jquery arrays json

我试图循环以下内容并在每次循环变量'identityindex'时存储count的值但我无法这样做。当我尝试查找存储在其中的值时,它返回为undefined。

其他一切都很好,代码少了这个部分。请问我能如何正确存储我的价值。我不能在这个实例中做一个全局变量声明,因为你可以看到我需要的信息根据我点击的输入而变化。

问题是identityindex没有获得任何值并返回为undefined。谢谢你的帮助。

var identity = [];
var count = 0;

$.getJSON("http://abcwebsite.com/public/user/listunpaidbills/",function(data){
        //Loop for each element on the data
        $.each(data,function(elem){
            var wrap = $("<div/>").attr('data-role', 'collapsible');
            identity.push(data[elem].id); 
            //Create the h1 and the other elements appending them to bills List
            $("<h1/>",{
                text:data[elem].reference
            }).appendTo(wrap);   
            $("<p/>",{
                text:"Account: "+ data[elem].account
            }).appendTo(wrap);        
            $("<p/>",{
                text:"Amount: "+ data[elem].amount
            }).appendTo(wrap);
            $("<p/>",{
                text:"ID: "+ data[elem].id
            }).appendTo(wrap);
            $("<button type='submit' value='Paid' class='myinput'/>",{
                data:{'identityindex':count}, //information not being assigned
                text:"Paid"
            }).appendTo(wrap);
            wrap.appendTo('#unpaidList');    
            count++;         
        })//end of for each loop 
        $( "#unpaidList" ).collapsibleset( "refresh" );

         //Confirm Payment
         $(".myinput").click(function(){  
            alert(identity[1]);
            var $this = $(this);          
            var index = $this.data('identityindex'); //undefined. 
            alert(index); 
            $.getJSON("http://abcwebsite.com/public/user/confirmbill/", {
                id: identity[index] 
                }, function(data) {
                    alert(data.status); 
                }).fail(function() {
                    alert("error");
            });
        });
    });

0 个答案:

没有答案