在 .append JQuery 中使用双引号和单引号将变量作为参数传递给 onclick 函数 [英] Pass variable as parameter to onclick function using double and single quotes in .append JQuery
问题描述
我正在尝试使用 JQuery 追加将变量作为参数传递给 onClick 函数.由于行数不确定,我使用 $.each 在每行的末尾添加一个按钮,单击该按钮将编辑该行.因此,为了获得该行,我将该行的 id 传递给按钮中的 Onclick 函数,我对引号感到困惑,因为该函数不起作用.
I am trying to pass a variable as a parameter to onClick function using JQuery append. as the number of rows are uncertain I am using $.each to add a button at the end of each row which when clicked will edit that row. So to get that row i am passing the id of that row to the Onclick function in a button, I am confused with the quotes as the function is not working.
这是代码
$.each(result, function (key, item) {
$("#table1").append("<tr><td>" + item.id + "</td><td>" + item.name + "</td> <td>" + item.type + "</td> <td>" + item.weight + "</td> <td>" + item.birthday + "</td> <td>" + item.hasHorns + "</td><td><input class='editR' id='" + item.id + "' type='button' value='EDIT' onclick='editRow('" + item.id + "')'/> </td> </tr>");
});
功能
function editRow(eid) {
$("#table1").empty();
//var eid = $(this).attr("id");
alert(this);
$.getJSON(uri + "GetByID/" + eid, function (data) {
$("#headerType").replaceWith("<h2>Edit Animal</h2>");
$("#table1").append("<tr><td><h4>ID</h4></td> <td><h4>Name</h4></td> <td><h4>Type</h4></td> <td><h4>Weight</h4></td> <td><h4>Birthday</h4></td> <td><h4>Has Horns</h4></td></tr>");
$("#table1").append("<tr><td>" + data.id + "</td><td>" + data.name + "</td> <td>" + data.type + "</td> <td>" + data.weight + "</td> <td>" + data.birthday + "</td> <td>" + data.hasHorns + "</td> </tr>");
});
}
浏览器检查元素的结果(对不起,我不能发布图片,因为它需要 10 次)
RESULT on Browser Inspect element (Sorry i cannot post pics as it needs 10 impoession)
<input class="editR" id="1" type="button" value="EDIT" onclick="editRow(" 1')'="">
一切正常,但点击后没有任何反应
everything works but nothing happens on click
谢谢
推荐答案
尝试更改为
onclick=\"editRow('" + item.id + "')\"/>
这篇关于在 .append JQuery 中使用双引号和单引号将变量作为参数传递给 onclick 函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!