[英]How to pass the value from one javascript function to another
我试图将值从一个函数传递给另一个 onclick
function getReporteeList() {
$.ajax({
url:'http://localhost:8088/JirasTrackingApp/reporter/Reportees/ReporteeList/'+ $("#ManagerId").val(),
type:'GET',
dataType: 'json',
success: function(result){
alert(result);
var content = '';
$.each(result,function(key,value){
content += '<tr>';
content += '<td>'+value.Name+'</td>';
content += '<td>'+value.UserId+'</td>';
content += '<td>'+'<a href="#" onclick ="getJira('+value.UserId+')">'+value.count+'</a>'+'</td>';
content += '</tr>';
});
$('#employee_table').append(content);
console.log(result);
}
});
}
function getJira(value)
{
var empid = document.getElementById("value").value;
console.log(empid);
}
单击 UserId 超链接时抛出错误:未捕获的 ReferenceError: at12345 is not defined at HTMLAnchorElement.onclick
value.userid
是一个字符串,因此您需要在getJira()
的参数getJira()
引号将其括起来。
content += '<td>'+'<a href="#" onclick ="getJira(\''+value.UserId+'\')">'+value.count+'</a>'+'</td>';
只需为您的<td>
添加一个事件侦听器并在那里获取值。 此外,如果您只是尝试调用函数,那么还有比锚元素更好的选项。
下面的解决方案只使用普通的旧 javascript(没有 jquery)。 基本上它会找到表employeeTable 内的所有跨度。 然后它添加一个点击侦听器并拉出 id。 您可以设置当前设置其他所有内容的方式。 您可以提取一个值,一个真正包含您的值的任何您想要的类。
例子:
var spans = document.getElementById('employeeTable').querySelectorAll('span'); spans.forEach(function(elem){ elem.addEventListener('click', function () { console.log(this.id); }, false); })
<table id="employeeTable"> <thead> <tr> <td>Name</td> <td>Class</td> <td>Link</td> <tr> </thead> <tbody> <tr> <td>Juice</td> <td>Math</td> <td><span id="t12345">here is the click</span></td> </tr> <tr> <td>Eggs</td> <td>English</td> <td><span id="t8675309">here is the click</span></td> </tr> </tbody> </table> <Br><br><br> <span>I'm a span outside of the table, I won't fire click listener</span>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.