0

我正在尝试将 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);
            }

我在控制台窗口中收到此错误“Uncaught SyntaxError: Unexpected end of input”。

4

1 回答 1

0

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

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

于 2020-06-01T07:42:04.700 回答