再会,
一段时间以来,我一直在努力做到这一点,我只是想知道我是在做一些非常愚蠢的事情还是在这方面。
该代码从 php 中获取一个 json 对象并正确附加信息。但我无法让它调用附加锚标记上的函数。我首先尝试通过 jQuery 通过其 id 使用 click 函数,但这不起作用,现在我试图通过 onclick 触发事件。
$(document).ready(function(){
$("#searchBtn2").click(function(){
$('#searchResults').children().remove();
var l = document.getElementById('properSearch').value.length;
if(l > 0){
var searchjson =
{
"search" : document.getElementById('properSearch').value
};
searchjs = JSON.stringify(searchjson);
var search = {json:searchjs};
$.ajax({
type: "POST",
url: "searchUsers.php",
data: search,
success: function(result)
{
var obj = jQuery.parseJSON(result);
$('#searchResults').next().remove();
for(var i = 0; i < obj.length; i+=1)
{
if(obj[i].user_id != localStorage.user_id)
$('#searchResults').append("<tr><td id = 'search_row'>" + obj[i].firstname + " " + obj[i].lastname + " </td> <td> <a OnClick = \"add();\" href = '#' id = 'friend_add' class = '" + obj[i].user_id + "' > add </a> </td> </tr> ");
}
},
error: function()
{
alert('An Error has occured, please try again.');
}
});
}
});
function add(){
alert("clicked");
//var theClass = this.className;
//alert( theClass );
};
});
编辑:具有开启功能
$(document).ready(function(){
$("#searchBtn2").click(function(){
$('#searchResults').children().remove();
var l = document.getElementById('properSearch').value.length;
if(l > 0){
var searchjson =
{
"search" : document.getElementById('properSearch').value
};
searchjs = JSON.stringify(searchjson);
var search = {json:searchjs};
$.ajax({
type: "POST",
url: "searchUsers.php",
data: search,
success: function(result)
{
var obj = jQuery.parseJSON(result);
$('#searchResults').next().remove();
for(var i = 0; i < obj.length; i+=1)
{
if(obj[i].user_id != localStorage.user_id)
$('#searchResults').append("<tr><td id = 'search_row'>" + obj[i].firstname + " " + obj[i].lastname + " </td> <td> <a href = '#' class = 'friend_add' id = '" + obj[i].user_id + "' > add </a> </td> </tr> ");
}
},
error: function()
{
alert('An Error has occured, please try again.');
}
});
}
});
});
$('#searchResults').on('click', '.friend_add', (function(){
alert("clicked");
)};
提前致谢