简体   繁体   English

从javascript代码中的td内的锚标记调用javascript函数

[英]Call a javascript function from anchor tag inside td inside javascript code

Only Basic javascript knowledge is required.If anyone knows please help me.只需要基本的javascript知识。如果有人知道请帮助我。 I don't know how to do it but i have tried it in many ways but still not able to pass the value.我不知道该怎么做,但我已经尝试了很多方法,但仍然无法传递该值。

Simply, i have a for loop and i have a td inside it.简单地说,我有一个 for 循环,里面有一个 td 。 I want to call a javascript function from this td click but problem is i am unable to pass any parameter to this function.我想从这个 td click 调用一个 javascript 函数,但问题是我无法将任何参数传递给这个函数。

the code is here :代码在这里:

 function GetContractList(abc) {
  var data = abc
  for (var it in data) {
            tab += "<tr>";
            tab += "<td>" + data[it].ContractCode + "</td>";
            if (data[it].ContractCode != "") {
                var Contract = data[it].ContractCode;
                 tab += "<td><a onclick='Delete_User(Contract);'>View</td>";
               // tab += "<td><a data=" + data[it].ContractCode + " href='javascript:Delete_User(this.data);'>View</td>";
            }
            else {
                tab += "<td></td>";
            }

As u can see, i have tried to pass parameter to Delete_User function but the syntax is somewhere broken.正如你所看到的,我试图将参数传递给 Delete_User 函数,但语法在某处被破坏了。

  tab += "<td><a onclick='Delete_User(Contract);'>View</td>";

this line gives error-- Contract is not defined.这一行给出了错误——合同未定义。

"<td><a data=" + data[it].ContractCode + " href='javascript:Delete_User(this.data);'>View</td>".

This line also doesnot passes any value to the function.该行也不向函数传递任何值。 Please someone help me out.请有人帮助我。

You need to escape the quote for the parameter of the function.您需要转义函数参数的引号。

tab += '<td><a href="javascript:Delete_User(\'' + data[it].ContractCode + '\');">View</a></td>';
//                                          ^^                             ^^

 function Delete_User(id) { console.log('Delete_User', id); } var cc = '4711abc', tab = '<a href="javascript:Delete_User(\\'' + cc + '\\');">View</a>'; document.write(tab);

try:尝试:
var cont = data[it].ContractCode.replace(/"/g, '\\\\"');
tab += '<td><a onclick="Delete_User(\\''+cont+'\\');">View</td>";

first line encodes any double quotes第一行编码任何双引号
the second line inserts the contract as a parameter and adds single quotes around it第二行将合约作为参数插入并在其周围添加单引号

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM