javascript 在for循环中用表调用onclick函数

wlwcrazw  于 2023-04-19  发布在  Java
关注(0)|答案(1)|浏览(98)

我试图有一个按钮在一个表列,当其点击,我想调用一个服务,从该行的值。可以有人请帮助实现这一点。

function setResponseData(data, length) {
        var insertHTML = "<TR class=\"heading\"><TD align=\"center\">&nbsp;&nbsp;TIMESTAMP&nbsp;&nbsp;</TD><TD align=\"center\">&nbsp;&nbsp;OPERATION&nbsp;&nbsp;&nbsp;TYPE&nbsp;&nbsp;</TD><TD align=\"center\">&nbsp;LOG &nbsp;&nbsp;LEVEL&nbsp;</TD><TD align=\"center\">&nbsp;&nbsp;VERSION&nbsp;&nbsp;</TD><TD align=\"center\">&nbsp;&nbsp;TYPE&nbsp;&nbsp;</TD><TD align=\"center\">&nbsp;&nbsp;SESSIONID&nbsp;&nbsp;</TD><TD align=\"center\">&nbsp;&nbsp;PIECEID&nbsp;&nbsp;</TD><TD align=\"center\">&nbsp;&nbsp;SHIPMENTID&nbsp;&nbsp;</TD><TD align=\"center\">&nbsp;SYSTEM &nbsp;ORIGIN&nbsp;</TD><TD align=\"center\">&nbsp;SYSTEM &nbsp;DESTINATION&nbsp;</TD><TD align=\"center\">&nbsp;SYSTEM &nbsp;DATAORIGIN&nbsp;</TD><TD align=\"center\">&nbsp;&nbsp;DIRECT &nbsp;INJECTION &nbsp;ORGANISATION&nbsp;&nbsp;</TD><TD align=\"center\">&nbsp;&nbsp;PRODUCT&nbsp;&nbsp;</TD><TD align=\"center\">&nbsp;&nbsp;STATUS&nbsp;&nbsp;</TD><TD align=\"center\">&nbsp;&nbsp;STATUS&nbsp; &nbsp;&nbsp;CODE&nbsp;&nbsp;</TD><TD align=\"center\">&nbsp;&nbsp;STATUS MESSAGE&nbsp;&nbsp;</TD><TD align=\"center\">&nbsp;&nbsp;NODENAME&nbsp;&nbsp;</TD><TD align=\"center\">&nbsp;&nbsp;PIECENUMBER&nbsp;&nbsp;</TD><TD align=\"center\">&nbsp;&nbsp;TOTALPIECES&nbsp;&nbsp;</TD><TD align=\"center\">&nbsp;&nbsp;SYSTEMNAME&nbsp;&nbsp;</TD><TD align=\"center\">&nbsp;&nbsp;INSERT &nbsp;&nbsp;TIMESTAMP&nbsp;&nbsp;</TD><TD align=\"center\">&nbsp;&nbsp;EVENT &nbsp;CODE&nbsp;&nbsp;</TD><TD align=\"center\">&nbsp;&nbsp;CUSTOMERID&nbsp;&nbsp;</TD><TD align=\"center\">&nbsp;&nbsp;PAYLOAD&nbsp;&nbsp;</TD></TR>";
        //var PayloadBtn = document.createElement("BUTTON");
        
        for (var i = 0; i < length; i++) {
            var audit = data.results[i];
            //var PayloadBtn = document.createElement("BUTTON");
            //PayloadBtn.innerHTML = audit.PAYLOAD;
            
            
            
            if ((i % 2) == 0) {
                insertHTML += "<TR style='background-color:#F0F0E0'>";
            } else {
                insertHTML += "<TR style='background-color:#E0E0C0'>";
            }

            if (Boolean(audit.TIMESTAMP)) {
                insertHTML += "<TD align=\"center\">&nbsp;" + audit.TIMESTAMP + "&nbsp;</TD>";
            } else {
                insertHTML += "<TD align=\"center\">&nbsp;</TD>";
            }
            if (Boolean(audit.OPERATIONTYPE)) {
                insertHTML += "<TD align=\"center\">&nbsp;" + audit.OPERATIONTYPE + "&nbsp;</TD>";
            } else {
                insertHTML += "<TD align=\"center\">&nbsp;</TD>";
            }
            if (Boolean(audit.LOGLEVEL)) {
                insertHTML += "<TD align=\"center\">&nbsp;" + audit.LOGLEVEL + "&nbsp;</TD>";
            } else {
                insertHTML += "<TD align=\"center\">&nbsp;</TD>";
            }
            if (Boolean(audit.VERSION)) {
                insertHTML += "<TD align=\"center\">&nbsp;" + audit.VERSION + "&nbsp;</TD>";
            } else {
                insertHTML += "<TD align=\"center\">&nbsp;</TD>";
            }
            if (Boolean(audit.TYPE)) {
                insertHTML += "<TD align=\"center\">&nbsp;" + audit.TYPE + "&nbsp;</TD>";
            } else {
                insertHTML += "<TD align=\"center\">&nbsp;</TD>";
            }
            if (Boolean(audit.SESSIONID)) {
                insertHTML += "<TD align=\"center\">&nbsp;" + audit.SESSIONID + "&nbsp;</TD>";
            } else {
                insertHTML += "<TD align=\"center\">&nbsp;</TD>";
            }
            if (Boolean(audit.PIECEID)) {
                insertHTML += "<TD align=\"center\">&nbsp;" + audit.PIECEID + "&nbsp;</TD>";
            } else {
                insertHTML += "<TD align=\"center\">&nbsp;</TD>";
            }
            if (Boolean(audit.SHIPMENTID)) {
                insertHTML += "<TD align=\"center\">&nbsp;" + audit.SHIPMENTID + "&nbsp;</TD>";
            } else {
                insertHTML += "<TD align=\"center\">&nbsp;</TD>";
            }
            if (Boolean(audit.SYSTEMORIGIN)) {
                insertHTML += "<TD align=\"center\">&nbsp;" + audit.SYSTEMORIGIN + "&nbsp;</TD>";
            } else {
                insertHTML += "<TD align=\"center\">&nbsp;</TD>";
            }
            if (Boolean(audit.SYSTEM_DESTINATION)) {
                insertHTML += "<TD align=\"center\">&nbsp;" + audit.SYSTEM_DESTINATION + "&nbsp;</TD>";
            } else {
                insertHTML += "<TD align=\"center\">&nbsp;</TD>";
            }
            if (Boolean(audit.SYSTEMDATAORIGIN)) {
                insertHTML += "<TD align=\"center\">&nbsp;" + audit.SYSTEMDATAORIGIN + "&nbsp;</TD>";
            } else {
                insertHTML += "<TD align=\"center\">&nbsp;</TD>";
            }
             if (Boolean(audit.DIRECT_INJECTION_ORGANISATION)) {
                insertHTML += "<TD align=\"center\">&nbsp;" + audit.DIRECT_INJECTION_ORGANISATION + "&nbsp;</TD>";
            } else {
                insertHTML += "<TD align=\"center\">&nbsp;</TD>";
            }
            if (Boolean(audit.PRODUCT)) {
                insertHTML += "<TD align=\"center\">&nbsp;" + audit.PRODUCT + "&nbsp;</TD>";
            } else {
                insertHTML += "<TD align=\"center\">&nbsp;</TD>";
            }
            if (Boolean(audit.STATUS)) {
                insertHTML += "<TD align=\"center\">&nbsp;" + audit.STATUS + "&nbsp;</TD>";
            } else {
                insertHTML += "<TD align=\"center\">&nbsp;</TD>";
            }
            if (Boolean(audit.STATUS_CODE)) {
                insertHTML += "<TD align=\"center\">&nbsp;" + audit.STATUS_CODE + "&nbsp;</TD>";
            } else {
                insertHTML += "<TD align=\"center\">&nbsp;</TD>";
            }
            if (Boolean(audit.STATUS_MESSAGE)) {
                insertHTML += "<TD align=\"center\">&nbsp;" + audit.STATUS_MESSAGE + "&nbsp;</TD>";
            } else {
                insertHTML += "<TD align=\"center\">&nbsp;</TD>";
            }
            if (Boolean(audit.NODE_NAME)) {
                insertHTML += "<TD align=\"center\">&nbsp;" + audit.NODE_NAME + "&nbsp;</TD>";
            } else {
                insertHTML += "<TD align=\"center\">&nbsp;</TD>";
            }
            if (Boolean(audit.PIECENUMBER)) {
                insertHTML += "<TD align=\"center\">&nbsp;" + audit.PIECENUMBER + "&nbsp;</TD>";
            } else {
                insertHTML += "<TD align=\"center\">&nbsp;</TD>";
            }
            if (Boolean(audit.TOTALPIECES)) {
                insertHTML += "<TD align=\"center\">&nbsp;" + audit.TOTALPIECES + "&nbsp;</TD>";
            } else {
                insertHTML += "<TD align=\"center\">&nbsp;</TD>";
            }
            if (Boolean(audit.SYSTEM_NAME)) {
                insertHTML += "<TD align=\"center\">&nbsp;" + audit.SYSTEM_NAME + "&nbsp;</TD>";
            } else {
                insertHTML += "<TD align=\"center\">&nbsp;</TD>";
            }
            if (Boolean(audit.INSERT_TIMESTAMP)) {
                insertHTML += "<TD align=\"center\">&nbsp;" + audit.INSERT_TIMESTAMP + "&nbsp;</TD>";
            } else {
                insertHTML += "<TD align=\"center\">&nbsp;</TD>";
            }
            if (Boolean(audit.EVENT_CODE)) {
                insertHTML += "<TD align=\"center\">&nbsp;" + audit.EVENT_CODE + "&nbsp;</TD>";
            } else {
                insertHTML += "<TD align=\"center\">&nbsp;</TD>";
            }
            if (Boolean(audit.CUSTOMERID)) {
                insertHTML += "<TD align=\"center\">&nbsp;" + audit.CUSTOMERID + "&nbsp;</TD>";
            } else {
                insertHTML += "<TD align=\"center\">&nbsp;</TD>";
            }
            if (Boolean(audit.PAYLOAD)) {
                insertHTML += "<TD><input class='myclass' type='button' value='Download' onclick =getPayload(${data.results[i]})/></TD>";
           } else {
                insertHTML += "<TD align=\"center\">&nbsp;</TD>";
            }
        }
        document.getElementById("contentTable").innerHTML = insertHTML;
    }

function getPayload(audit){

var param = "?";
        var flag = "false";
        if (audit.VERSION != "" && audit.SESSIONID!= "" && audit.SHIPMENTID!="" && audit.SYSTEM_NAME!="" && audit.TYPE!="") {
        
            param += "shipmentId=" + audit.SHIPMENTID;
            param += "version=" + audit.VERSION;
            param += "sessionId=" +audit.SESSIONID;
            param += "systemName=" + audit.SYSTEM_NAME;
            param += "messageType=" + audit.TYPE;
            param += "operationType=" + audit.OPERATIONTYPE;
            flag = "true";
        }

        var err = document.getElementById("errorContent");
        err.style.display = "none";

        var XHR = new XMLHttpRequest();
       
        // We setup our request
        XHR.open("GET", "/restv2/getPayloadml" + param);

我想在点击下载按钮时调用getPayload函数,但它没有调用该函数。
我的下载按钮在for循环中,并显示在表列中。当我单击某个特定按钮时,我希望将该行值发送到函数getPayload,因此我给出了onclick =getPayload(${data.results[i].我在这里做错了一些事情,没有调用该函数。
enter image description here

ccrfmcuu

ccrfmcuu1#

在浏览器的DOM资源管理器中,检查以下字符串的结果:

"<TD><input class='myclass' type='button' value='Download' onclick =getPayload(${data.results[i]})/></TD>"

你会发现,从字面上看,它是这样的:

<TD><input class='myclass' type='button' value='Download' onclick =getPayload(${data.results[i]})/></TD>

这是不正确的:

  1. onclick属性值周围没有引号。
  2. ${data.results[i]}是一个语法错误。
    看起来你是在尝试使用模板字面量。你还应该在属性值周围使用双引号,而不是单引号。所以字符串字面量(模板字面量)应该是这样的:
`<TD><input class="myclass" type="button" value="Download" onclick ="getPayload(${data.results[i]})" /></TD>`

请注意使用反引号来 Package 文字和使用双引号来 Package 属性值。浏览器对允许单引号非常宽容,但通常情况下,当您的代码没有按照您的预期运行时,首先要做的是确保它至少在技术上语法正确。

相关问题