将json数据传递给onclick jQuery函数

问题描述 投票:0回答:1

我正在尝试将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:意外的输入结束”。

javascript php jquery jquery-ui
1个回答
0
投票

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

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

作为:

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

原因是您正在调用函数showcomplaints,该函数需要id,而不是您传递了字符串v.ID

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

© www.soinside.com 2019 - 2024. All rights reserved.