0

我目前遇到了一些问题,基本上我创建了一些 populateModal() 函数来使用相应的 html 填充模态容器。它适用于 populateModal3 和 populateModal4 (相同的功能但具有不同的 url 值)。

ajax 以 json 格式返回数据,并且在其他两个 populateModal 函数中运行良好。

这是函数的片段

$.ajax({
                url: "transaction/get_transaction",
                dataType: 'json'
            }).done(function(data){
                $('#transModalContainerDel, #transModalContainerEdit').empty();
                console.log(data);
                for (var i = 0; i < data.length; i++)
                {                   
                    console.log('id data: ' + data[i].id);
                    theDelTransModal += 'id modal: ' + data[i].id;
                    //theDelTransModal += '<div id="transModalDel' + data[i].id + '" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="deleteTransaction" aria-hidden="true">';
                    //theDelTransModal += '<div class="modal-header"><button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button><h3 id="myModalLabel">Delete transaction?</h3></div>';
                    //theDelTransModal += '<div class="modal-body"><p>Are you sure you want to delete this transaction? Note that you can\'t undo this action.</p></div>';
                    //theDelTransModal += '<div class="modal-footer"><a class="btn" data-dismiss="modal" aria-hidden="true">Cancel</a><a href="transaction/delete_transaction/' + data[i].id + '" class="btn btn-primary" id="delete-trans" data-dismiss="modal">Delete</a></div>';
                    //theDelTransModal += '</div>';
                    //$('#transModalContainerDel').append(theDelTransModal);
                    console.log(theDelTransModal);
                }       
            });

从上面的代码可以看出,返回的数据是正确的 json 格式。但我尝试记录的 id 返回此值。

id data: 21 
id modal: 21 
id data: 22 
id modal: 21id modal: 22 

我不确定出了什么问题,因为此功能非常适合其他网址。

提前致谢

4

2 回答 2

0

只是因为字符串连接

theDelTransModal += 'id modal: ' + data[i].id;

等于:

theDelTransModal = theDelTransModal+ 'id modal: ' + data[i].id;

通过删除加号就可以了。

于 2013-01-01T05:38:03.557 回答
0

使用ajax成功方法

$.ajax({
                url: "transaction/get_transaction",
                dataType: 'json',
                success:function(data){

$('#transModalContainerDel, #transModalContainerEdit').empty();
                console.log(data);
                for (var i = 0; i < data.length; i++)
                {                   
                    console.log('id data: ' + data[i].id);
                    theDelTransModal += 'id modal: ' + data[i].id;
                    //theDelTransModal += '<div id="transModalDel' + data[i].id + '" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="deleteTransaction" aria-hidden="true">';
                    //theDelTransModal += '<div class="modal-header"><button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button><h3 id="myModalLabel">Delete transaction?</h3></div>';
                    //theDelTransModal += '<div class="modal-body"><p>Are you sure you want to delete this transaction? Note that you can\'t undo this action.</p></div>';
                    //theDelTransModal += '<div class="modal-footer"><a class="btn" data-dismiss="modal" aria-hidden="true">Cancel</a><a href="transaction/delete_transaction/' + data[i].id + '" class="btn btn-primary" id="delete-trans" data-dismiss="modal">Delete</a></div>';
                    //theDelTransModal += '</div>';
                    //$('#transModalContainerDel').append(theDelTransModal);
                    console.log(theDelTransModal);
                }       
}
            });
于 2013-01-01T05:38:43.533 回答