我正在尝试将多个变量作为参数发送给在onClick事件上调用的函数( redi )。目前,我仅发送一个( id ),找不到发送多个邮件的方法(我想向其发送 id,nome,cognome )。我已经尝试过使用 onclick ='redi(this.id,nome,cognome)',但是由于引号(nome,cognome是字符串),这似乎是一个错误。
$.ajax({
url: "<URL to backend script>",
dataType: 'jsonp',
jsonp: 'jsoncallback',
timeout: 5000,
success: function(data, status){
$.each(data, function(i,item)
{
var id = item.registrationID;
var nome = item.registrationName;
var cognome = item.registrationSurname;
var id_referenza = item.registrationUsername;
document.getElementById('divPaziente').innerHTML += "<a href='"+nome+"'><button class='button button-block button-positive' id='"+id+"' onclick='redi(this.id)'>"+nome+' '+cognome+' - '+id_referenza+"</button></a>";
window.redi = function(elem)
{
alert("elem: "+elem);
localStorage.setItem("id", elem);
}
});
},
error: function()
{
output.text('There was an error while loading data.');
}
});
答案 0 :(得分:0)
尝试使用“数据”属性。
<a href='"+nome+"'><button class='button button-block button-positive' id='"+id+"' data-nome="'+nome+'" onclick='redi(this.id,this.getAttribute("data-nome"))'>"+nome+' '+cognome+' - '+id_referenza+"</button></a>
注意:请避免使用innerHTML,而应使用appendChild()
答案 1 :(得分:0)
使用转义字符将其他变量作为字符串传递
redi(this.id,\'' + nome + '\',\'' + cognome + '\')