0

附加是正确的及其填充列表视图。我想要的是在单击时获取每个列表视图的索引。但它在我的警报消息中始终为 0 索引。如何获取每个列表视图的索引?谢谢

html代码

<ul id="RecipeList" data-role="listview" data-split-icon="star" data-split-theme="a" data-inset="true "href="#recpdetail">                 
</ul>

JS代码

for(var i=0; i<NameArr.length; i++){
    var $content = $('<li><a href="#recpdetail"><img src="../../img/album-bb.jpg"><h3>Name: '+ NameArr[i] + '</h3><p>Code: '+ CodeArr[i]+ '</p><p>Category: '+ CatArr[i]+ '</p></a><a href="#purchase" data-rel="popup" data-position-to="window" data-transition="pop">Add to favorites</a></li>');                              
    $('#RecipeList').append($content).listview('refresh');
}

索引代码

$("#RecipeList").click(function(){
    var selected_index = $(this).find('li').index();
    alert('Selected Index = ' + selected_index);
});      
4

1 回答 1

1

这里的问题是你总是得到第一个li元素的索引而不是 clicked li

更改您的点击事件以定位li内部元素RecipeList,如下所示

$("#RecipeList").on('click', 'li', function(){
    var selected_index = $(this).index();
    alert('Selected Index = ' + selected_index);
});  
于 2013-04-26T07:01:39.400 回答