点击<a>
代码&#34;添加为朋友&#34;后,如何检索电子邮件值当我们收到电子邮件时,请更改<a>
代码&#34;添加为朋友&#34;发送给朋友请求&#34; ?
找到-friends.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Find Friends</title>
</head>
<body>
<h4><a href="php/logout.php">Log Out</a> </h4>
<table class="details" align="center">
</table>
<script src="js/jquery-1.10.1.min.js"></script>
<script type="text/javascript" src="js/find-friends.js"></script>
</body>
</html>
找到-friends.js
$(document).ready(function() {
$.getJSON('php/find-friends.php', function( data ) {
$.each(data, function(key, value) {
var tr=$('<tr></tr>');
$.each(value, function(key, val) {
if (key === 'name') {
// $('#name').append('Name:'+ ' ' + val);
$('<td>'+"Name : "+val+ ' ' +'</td>').appendTo(tr);
}
if (key === 'mobile') {
$('<td>'+"Mobile : "+val+ ' ' +'</td>').appendTo(tr);
}
if (key === 'email') {
$('<td>'+"Email : "+val+' '+'</td>').appendTo(tr);
// $('<td>'+'<a href=# class=link>'+'Add as Friend'+'</a>'+'</td>').appendTo(tr);
$('<tr><td>'+'<a href="#" class="link">'+"Add as Friends"+'</a>'+'</td>').appendTo(tr);
}
});
tr.appendTo('.details');
});
});
});
答案 0 :(得分:0)
将电子邮件作为data
属性添加到链接:
if (key === 'email') {
$('<td>'+"Email : "+val+' '+'</td>').appendTo(tr);
// $('<td>'+'<a href=# class=link>'+'Add as Friend'+'</a>'+'</td>').appendTo(tr);
$('<tr><td>'+'<a href="#" class="link" data-email="' + val + '">'+"Add as Friends"+'</a>'+'</td>').appendTo(tr);
}
然后你可以用它来检索它:
$(".details").on("click", ".link", function() {
var email = $(this).data("email");
sendFriendRequest(email);
$(this).text("Friend request sent");
});