2

New Bie : json を使用してデータベースから取得し、formatリストビューに表示するリスト データがあります。しかし、リストビューの選択されたインデックスを取得する方法をまだ混乱させています。これは私のコードです:

<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>

誰かが私を助けてくれることを願っています。

4

2 に答える 2

4

イベントliをバインドするために選択したインデックスは必要ありません。clickイベント委任を使用して、クリック イベントをバインドしてみてください。クリック イベントを にバインドし、ulにデリゲートしliます。

$("#penyakit_list").on("click", "li a", function() {
  //your code
}); 

もう1つのオプションは、クリックをにバインドすることdocumentです:

$(document).on("click", "#penyakit_list li a", function() {
  //your code
}); 

インデックスにアクセスするには、クリック イベント内でこれを使用します。

$(this).closest("li").index();

クリックしthisた はどこですか。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.
}); 
于 2013-07-30T16:32:38.053 に答える
1

+1 は bundleofjoy に行きます!

私の「評判ポイント」がコメント追加の上限値を下回っているため、コメントを追加できません。

私はこの答えを次のように使用しましたalert($(this).closest("li").attr("id"));

良い一日を過ごしてください!

于 2014-08-26T07:01:09.970 に答える