[英]How do i use onclick() method properly when passing some variable arguements i.e the arguements in this case is an ID of a Mongoose Object Document?
So I'm dynamically appending rows to a table on front-end, based on the array I get from a node.js API I wrote.因此,我根据从我编写的 node.js API 获得的数组动态地将行附加到前端的表中。 The array indicates students.该数组表示学生。 For each row I append and display Name , Class , Roll_No and a dynamic button whose color changes depending on the value I get from that specific student.对于每一行,我附加并显示Name 、 Class 、 Roll_No和一个动态按钮,其颜色根据我从该特定学生获得的值而变化。 Now for that button I use the following tag:现在对于那个按钮,我使用以下标签:
$('#users').append("<tr><td>" + (i + 1) + "</td><td>" + val.first_name + " " + val.last_name + "</td><td>" + val.roll_no + "</td><td>" + val.class + "</td><td><button id='tempButton' class='btn btn-danger' onclick='add_remove_student(" + val._id + ", " + val.is_linked + ")'>DELETE</button></td></tr>");
val
indicates the current element of the students array, which is a mongoose object. val
表示学生数组的当前元素,它是一个猫鼬对象。 Now I get an error whenever I click the appended button and the add_remove_student()
method is not called.现在,每当我单击附加按钮并且未调用add_remove_student()
方法时,我都会收到错误add_remove_student()
。 The error says:错误说:
uncaught SyntaxError: Invalid or unexpected token未捕获的语法错误:无效或意外的令牌
val._id
is a Mongoose ID of that particular Mongoose object and val.is_linked
is a boolean value. val._id
是该特定 Mongoose 对象的 Mongoose ID,而val.is_linked
是布尔值。
The onclick()
in the button tag worked before I switched to node.js在我切换到 node.js 之前,按钮标签中的onclick()
起作用了
I've scratched my head so many times as to what I'm doing wrong.对于我做错了什么,我已经多次挠头。 Any help would be greatly appreciated!任何帮助将不胜感激!
The short answer: don't use onclick
, or any other of the onX
inline event attributes.简短的回答:不要使用onclick
或任何其他onX
内联事件属性。 They are outdated, lead to spaghetti code and don't follow the separation of concerns principle.它们已经过时,导致意大利面条式代码并且不遵循关注点分离原则。
A much better way to achieve this would be to just add the relevant meta data to the element as data
attributes.实现这一点的更好方法是将相关元数据作为data
属性添加到元素中。 Then, using a single delegated event handler, you can read that metadata from the element which was clicked.然后,使用单个委托事件处理程序,您可以从单击的元素中读取该元数据。 Try this:尝试这个:
let i = 0; let val = { _id: "5e4780d3cfbe57182499506a", role: ["STUDENT"], is_active: true, linked_students: [], first_name: "test_fname", last_name: "test_lname", email: "t1@t.com", roll_no: "537", class: 7, description: "im a test user mate!", created_at: "2020-02-15T05:25:39.077Z", updatedAt: "2020-02-15T05:25:39.077Z", v: 0, is_linked: true } $('button').on('click', function() { $('#users').append(`<tr><td>${(++i)}</td><td>${val.first_name} ${val.last_name}</td><td>${val.roll_no}</td><td>${val.class}</td><td><button class="delete btn btn-danger" data-id="${val._id}" data-linked="${val.is_linked}">DELETE</button></td></tr>`); }); $('#users').on('click', '.delete', function() { let $btn = $(this); let id = $btn.data('id'); let isLinked = $btn.data('linked'); console.log(id, isLinked); // do something with the above data... $(this).closest('tr').remove(); });
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <button>Append</button> <table id="users"></table>
As a side note, don't use id
attributes in content which will be dynamically repeated.作为旁注,不要在将动态重复的内容中使用id
属性。 id
need to be unique in the DOM. id
需要在 DOM 中是唯一的。 Use class
attributes instead.请改用class
属性。
If, for whatever reason, you absolutely had to use an inline event attribute you need to fix your quotes so that they match, and you also need to escape the quotes you use in the onclick
value so they don't interfere with the outer string:如果,无论出于何种原因,您绝对必须使用内联事件属性,您需要修复您的引号以使其匹配,并且您还需要转义您在onclick
值中使用的引号,以便它们不会干扰外部字符串:
$('#users').append('<tr><td>' + (i + 1) + '</td><td>' + val.first_name + ' ' + val.last_name + '</td><td>' + val.roll_no + '</td><td>' + val.class + '</td><td><button id="tempButton" class="btn btn-danger" onclick="add_remove_student(\"' + val._id + '\", \"' + val.is_linked + '\")">DELETE</button></td></tr>');
Okay so I've wrecked my brain for 24 hrs to solve this and after understanding how escape quotes characters work i've finally solved it with the following code:好的,所以我已经破坏了我的大脑 24 小时来解决这个问题,在了解转义引号字符的工作原理后,我终于用以下代码解决了它:
$('#users').append(`<tr><td>` + (i + 1) + `</td><td>` + val.first_name + ` ` + val.last_name + `</td><td>` + val.roll_no + `</td><td>` + val.class + `</td><td><button id="tempButton" class="btn btn-danger" onclick='add_remove_student(\"` + val._id + `\",\"` + val.is_linked + `\")'>DELETE</button></td><tr>`);
Thank You ^^谢谢你^^
This is the issue of mixing of quotes please re verify the quotes on the function calling in jquery.这是引号混合的问题,请重新验证jquery中调用函数的引号。 Plus please made an id of the element dynamic.另外,请动态设置元素的 id。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.