简体   繁体   English

数据库中的项目与onclick之间的链接

[英]Link between items from database and onclick

my app using cordova3.4, I display list of users from database lets say user1, user2 and user3 I want, if click on user1 then display user1 info. 我的应用程序使用cordova3.4,我显示数据库中的用户列表,说我想要的user1,user2和user3,如果单击user1,则显示user1信息。 if click on user2 then display user2 info. 如果单击user2,则显示user2信息。 if click on user3 then display user3 info. 如果单击user3,则显示user3信息。

I have for loop which display the three users, how to link between onClick event and user Id. 我有显示三个用户的for循环,以及如何在onClick事件和用户ID之间进行链接。

function querySuccess(tx, results) {
var len = results.rows.length;
for (var i=0; i<len; i++){        
var userId = results.rows.item(i).user_id;

var div1 = document.createElement("div");
div1.setAttribute('class', 'left');
var a1 = document.createElement("a");
a1.setAttribute('onClick', "showUserProfile(userId)");
....
....
}
}

function showUserProfile(userId)
{
alert("user Id: " +userId);
}

Hey why you using create div using javascript here , just use jquery 嘿,为什么在这里使用javascript使用create div,只需使用jquery

    var html="";
    for (var i=0; i<len; i++){        
    var userId = results.rows.item(i).user_id;

    html+="<div class='left' onclick='showUserProfile("+userId+")'></div>"


    }
    $("#id").append(html);//  where you append this element 

now you write function 现在你写功能

function showUserProfile(userId)
{
alert("user Id: " +userId);
}

ref 参考

The issue is probably that userId is not actually being passed here: 问题可能是实际上没有在此处传递userId

a1.setAttribute('onClick', "showUserProfile(userId)");

You should instead revise this line similar to how the other answer shows, but here it is without jQuery add: 您应该修改此行,类似于其他答案的显示方式,但是此处没有jQuery add:

a1.setAttribute('onClick', "showUserProfile(" + userId + ")");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM