将 json 数据传递给 onclick jquery 函数

pass json data to onclick jquery function

我正在尝试将 JSON 对象传递给 onclick 函数,但它不起作用

              $.each(response, function(k, v) {
                    html += "<tr>";
                    html += "   <td><a  onclick='showcomplaints('+ v.ID +')'  >" + v.ID + "</a></td>";
                    html += "   <td>" + v.Name + "</td>";
                    html += '  </tr>';
               });

            function showcomplaints(id)
            {
             alert(id);
            }

我在控制台 window 中收到此错误 "Uncaught SyntaxError: Unexpected end of input"。

这里您需要在代码中修复这一行:

html += "   <td><a  onclick='showcomplaints('v.ID')'  >" + v.ID + "</a></td>";

作为:

html += "   <td><a  onclick='showcomplaints("+v.ID+")'  >" + v.ID + "</a></td>";

原因是您正在调用一个需要 id 的函数 showcomplaints 而不是这个您传递的是字符串 v.ID

希望这对你以后有帮助:https://www.w3schools.com/js/js_strings.asp