如何在jQuery Mobile中传递值? [英] How to pass values in jQuery Mobile?
问题描述
在使用PhoneGap时,我创建了一个SQLite数据库,该数据库存储名称和电话号码以及一个自动递增的id字段.我正在使用jQuery Mobile来获取结果并将其显示在列表视图中,这是代码
While working with PhoneGap I have created a SQLite database that stores the name and phone number along with an autoincrementing id field. I am using jQuery Mobile to fetch the result and display it in a listview, here is the code
function queryDB(tx){
tx.executeSql('SELECT * FROM DEMO',[],querySuccess,errorCB);
}
function querySuccess(tx,result){
$('#contactList').empty();
$.each(result.rows,function(index){
var row = result.rows.item(index);
$('#contactList').append('<li><a href="#"><h3 class="ui-li-heading">'+row['name']+'</h3><p class="ui-li-desc">Phone '+row['phone']+'</p></a></li>');
});
$("#contactList").listview();
}
现在,我希望当用户点击屏幕上的某些列表元素时,应该将他带到新页面.与此同时,我想将该用户的ID传递到新页面,以便可以使用该ID在数据库中保存更多详细信息.由于我是JQM的新手,所以我不知道该怎么做
Now I want that when the user tap on certain list element on the screen he be should taken to a new page. Along with that I want to pass the id of that user to the new page so that I can save more details in the DB using that ID. As I am new to JQM, I am not able to figure out how to do it
推荐答案
您需要对所有LI发起攻击的第一件事
First thing you need to attack the event to all LI
$('#contactsList li').on("vclick",function(){
//do what you need to do to change the page
});
在li内的每个链接上,您还缺少'data-role ="button"'.我会这样更改它们:
You're also missing the 'data-role="button"' on each of the links inside li. I would change them like this:
$('#contactList').append('<li><a href="#" id='+row['id']+' data-role="button"><h3 class="ui-li-heading">'+row["name"]+'</h3><p class="ui-li-desc">Phone '+row["phone"]+'</p></a></li>');
最后:
$('#contactsList li').on("vclick",function(){
localStorage.currentId = $('a',this).attr("id");
$.mobile.changePage('myOtherPage.html');
});
要从其他页面访问ID,请执行以下操作:
To access the id from the other page:
var currentId = localStorage.currentId;
这篇关于如何在jQuery Mobile中传递值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!