如何在项目点击时获取项目 ID,我能够在 Listview 中显示数据我想点击项目显示我得到特别news_id的警报。
这是我的 HTML 页面:
<body>
 <div data-role="page" id="taxmanhomepage"  data-theme="e">
  <div data-role="header" data-position="fixed" data-tap-toggle="false" data-theme="e">
   <h4 align="center">Taxmann Demo App</h4>
  </div>
  <div data-role="content" data-theme="e">
   <a data-role="button" onclick="callservice()">Webservice</a> 
   Todays Headlines:
   <div class="content-primary">
    <ul id="newlist" data-role="listview" data-inset="true"data-filter-theme="e" data-divider-theme="e">
    </ul>
   </div>
  </div>
 </div>
</body>
这是我的 JavaScript 代码:
var URL="http://www.taxmann.com/TaxmannWhatsnewService/mobileservice.aspx?service=corporatelaws";
var news_id=null;
var news_title=null;
var news_short_description=null;
var new_hash = {};
document.addEventListener("deviceready", onDeviceReady, false);
function onDeviceReady() {}
function backToHome() {
    $.mobile.changePage('#taxmanhomepage', {
        reverse : false,
        changeHash : false
    });
}
$(document).on('pagebeforeshow','#newslistpage',function(event){
  $('#newlist').empty();
});
function callservice(){
 $.ajax({
  type : "GET",
  url : URL,
  dataType : "json",
  cache : false,
  error : function(xhr, ajaxOptions, thrownError) {
    debugger;
  },
  success : function(response, status, xhr) {
    response=xhr.responseText;
    var responseArray = JSON.parse(response);
    console.log(responseArray);
    var length = responseArray.length;
    var html='';
    for(i=0;i<length;i++) {
     news_id=$.trim(responseArray[i].news_id);
     news_title=$.trim(responseArray[i].news_title);
     news_short_description=$.trim(responseArray[i].news_short_description);
     new_hash[news_id]=[news_title,news_short_description];
     $("#newlist").append("<li 'style='font-size:10pt';'font-weight:bold';' ><a href='#'  ><h1 class='myHeader'>"+news_title+"</h1><br><h6 class='myHeader'>"+news_short_description+"</h6></a></li>");
  }
  $("#newlist").append("</ul>");
  $('#newlist').listview('refresh');
  $('#newlist').children('li').on('click', function (){
   var index = $(this).index();
   //var text = $(this).text();
   alert(index);
  });
 }
 });  
}