2013-05-13 59 views
0

如何獲得項目點擊項目ID,我能夠在Listview上顯示數據我想單擊項目顯示,我得到特別news_id警報。如何獲得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); 
    }); 
} 
}); 
} 
+2

你不是已經問這個問題[點擊這裏?](http://stackoverflow.com/q/16515225/1612146) – George 2013-05-13 08:54:42

+0

是的,我問,但這是有點不同但現在我能夠申請上單擊功能在這裏問題是我將如何獲得news_id警報在項目單擊 – user2372154 2013-05-13 08:56:25

+0

刪除重複:http://stackoverflow.com/questions/16515225/how-to-apply-onitem-click-using-java-script – diEcho 2013-05-13 08:57:31

回答

0

改變你的代碼是這樣的追加部分,

$("#newlist").append($("<li/>").data("newsId", news_id).html("<a href='#' ><h1 class='myHeader'>" + news_title + "</h1><br/><h6 class='myHeader'>" + news_short_description + "</h6></a>")); 

並改變這樣的單擊事件,

$('#newlist').on('click', 'li',function() { 
    var index = $(this).index(); 
    //var text = $(this).text(); 
    alert($(this).data("newsId")); 
}); 
1

news_idli項目的data-attribute,然後檢索:

$("#newlist").append("<li data-news-id='" + news_id + "' rest of string"); 

$('#newlist').children('li').on('click', function(){ 
    var news_id = $(this).attr('data-news-id'); 
    alert(news_id); 
}); 

如果您不希望該屬性可見,那麼使用data()

$("<li rest of string").data('news_id', news_id).appendTo($('#newlist')); 

$('#newlist').children('li').on('click', function(){ 
    var news_id = $(this).data('news_id'); 
    alert(news_id); 
}); 
+0

我不想在ListView上顯示News_id我只能顯示news_title和news_showrt Descriton – user2372154 2013-05-13 08:58:25

+0

這不是Konstantin指出的......閱讀他的答案。測試代碼! – Imperative 2013-05-13 08:59:35

+0

其工作但我不想在列表視圖中顯示news_id – user2372154 2013-05-13 09:02:48

相關問題