Android 如何获取所选索引JQuery Mobile Listview

Android 如何获取所选索引JQuery Mobile Listview,android,jquery,jquery-mobile,Android,Jquery,Jquery Mobile,新Bie:我有一个使用jsonformat从数据库获取的列表数据,并显示在listview中。但我仍然不知道如何获取listview的选定索引,这是我的代码: <script> function loadListTips(){ $('#message').html('Loading...'); $.ajax({ type: 'get', url: "http://10.0.2.2/compfest/ajax/belajar/li

新Bie:我有一个使用json
format
从数据库获取的列表数据,并显示在listview中。但我仍然不知道如何获取listview的选定索引,这是我的代码:

<script>
    function loadListTips(){
    $('#message').html('Loading...');
    $.ajax({
        type: 'get',
        url: "http://10.0.2.2/compfest/ajax/belajar/list_tips.php",
        success: function(response){
            var html = '';
            $.each(response, function(index,item){
                html += '<li>';
                html += '<a>';
                html += '<span class="judul">' + item.judul + '</span>';
                html += '</a>';
                html += '</li>';
            });
            $('#penyakit_list').html(html);
            $('#penyakit_list').listview('refresh');
            $('#message').html('');
        },
        error: function(e){
            alert('Error: ' + e);
        }
    });
}
</script>

函数loadListTips(){
$('#message').html('Loading…');
$.ajax({
键入:“get”,
url:“http://10.0.2.2/compfest/ajax/belajar/list_tips.php",
成功:功能(响应){
var html='';
$。每个(响应、功能(索引、项目){
html+=“
  • ”; html+=''; html+=''+item.judul+''; html+=''; html+='
  • '; }); $('penyakit_list').html(html); $('penyakit_list')。列表视图('refresh'); $('#message').html(''); }, 错误:函数(e){ 警报('错误:'+e); } }); }

    希望有人能帮助我

    您不需要选择
    li的索引来绑定
    单击
    事件。您可以尝试使用事件委派来绑定单击事件。将单击事件绑定到
    ul
    并将其委托给
    li

    $("#penyakit_list").on("click", "li a", function() {
      //your code
    }); 
    
    另一个选项是将单击绑定到
    文档

    $(document).on("click", "#penyakit_list li a", function() {
      //your code
    }); 
    
    要访问索引,只需在click事件中使用:

    $(this).closest("li").index();
    
    其中
    是您刚才单击的
    a
    。因此,现在您的单击事件将如下所示:

    $(document).on("click", "#penyakit_list li a", function() {
          alert($(this).closest("li").index()); // this will give the index of the li. You could store it in a variable and send it via ajax.
    }); 
    

    +1去bundleofjoy

    我无法添加评论,因为我的“声誉点”低于添加评论的限制值

    我使用这个答案作为
    警报($(this).nearest(“li”).attr(“id”)


    祝你有愉快的一天

    “listview的选定索引”-这是什么意思?您是否正在尝试为listview中的
    a
    标记编写
    click
    事件?是的,我想尝试如何基于索引为listview编写click事件。是否需要使用索引?实际上它有点不需要..为什么需要索引?因为索引值将通过Ajax发送到服务器。服务器将根据索引处理请求。谢谢你的回答。现在检查我的更新答案