简体   繁体   English

从JavaScript动态添加tr元素的onclick事件

[英]Add onclick event for tr element dynamically from JavaScript

I am creating a table dynamically, I need to add an onclick event for each element as it is added, but this needs a dynamic parameter add, I have tried the following 我正在动态创建表,我需要为添加的每个元素添加一个onclick事件,但这需要添加动态参数,我已经尝试了以下方法

trElements[i + 1].onclick = function () { 
    navigateToController('/Home/Client', "'" + machine.DeviceID + "'"); 
};

but this shows the onclick event as navigateToController('/Home/Client', "'" + machine.DeviceID + "'") 但这会将onclick事件显示为navigationToController('/ Home / Client',“'” + machine.DeviceID +“'”)

and not navigateToController('/Home/Client', 'DeviceName'); 而不是navigationToController('/ Home / Client','DeviceName'); as I thought it would, I have also tried to have the onclick event in the html, and replace the DEVICEID with the actual deviceid. 正如我想的那样,我也尝试过在HTML中包含onclick事件,并将DEVICEID替换为实际的deviceid。

var element = trElements[i + 1].outerHTML.replace('DEVICEID', machine.DeviceID);
trElements[i + 1].outerHTML = element; 

this shows up as been correct, but when the page is loaded, it still has deviceid in there ? 这显示是正确的,但是在加载页面时,页面中仍然有deviceid?

I am sure it is something really simple... but any pointers would be appreciated. 我确信这确实很简单...但是任何指针都将不胜感激。

I'm not entirely sure I understand the question, but it might be a scope issue. 我并不完全确定我了解这个问题,但这可能是范围问题。 You might need to use bind to create the handler with the current value of DeviceID : 您可能需要使用bind创建具有DeviceID当前值的处理程序:

 var machineId = 0; function navigateToController(machineId) { alert(machineId); } function addRow() { var t = document.getElementById('thetable'); var tr = t.insertRow(); var td = tr.insertCell(); td.appendChild(document.createTextNode("machine " + machineId)); tr.addEventListener('click', navigateToController.bind(null, machineId)); machineId++; } 
 <button onclick="addRow()">Add Row</button> <table id="thetable"></table> 

but this shows the onclick event as navigateToController('/Home/Client', "'" + machine.DeviceID + "'") 但这会将onclick事件显示为navigationToController('/ Home / Client',“'” + machine.DeviceID +“'”)

You're not going to see the value of the variable in the markup, if that's what you mean. 如果这就是您的意思,那么您将不会在标记中看到变量的值。

Why not just navigateToController('/Home/Client', machine.DeviceID ) ? 为什么不只是navigateToController('/Home/Client', machine.DeviceID ) It's already a string. 已经是一个字符串了。 No need to try to add quotes around it. 无需尝试在其周围添加引号。 You'd end up with a parameter that includes the quotes. 您最终将得到一个包含引号的参数。

 var deviceId = 'theDeviceId'; // This is not what you want. Notice this alert includes the single quotes as part of the string: "'theDeviceId'" alert( "'" + deviceId + "'"); // This is what you want. This alert has just the (unquoted) value. alert( deviceId ); 

The better way to do this is not to do it at all. 更好的方法是根本不执行此操作。

Better attach the event handler to existing container (the table in this case seems good) and check for bubbling events. 最好将事件处理程序附加到现有容器(在这种情况下,该表看起来不错)并检查冒泡事件。

With jQuery is quite simple: 使用jQuery非常简单:

var myTable = $("table#theTable");
myTable.on("click", "tr", function(){ // Click event handler.
    var clickedRow = $(this);
    ...
});

If you need some data related to each row, simply attach to it in "data-" attributes (ie «data-myId="someId"») of the tag. 如果需要与每一行相关的一些数据,只需将其附加到标签的“ data-”属性(即«data-myId =“ someId”»)中即可。 Then you could read them simply by clickedRow.data("myId") from event handler function. 然后,您可以通过事件处理函数中的clickedRow.data(“ myId”)轻松读取它们。

This way you have single function to handle all events for all rows. 这样,您就可以使用一个函数来处理所有行的所有事件。 Simpler and wasting too less mamory. 更简单,浪费的内存更少。

trElements[i + 1].onclick = (function () { 
    var deviceId = machine.DeviceID;
    return function(){
       navigateToController('/Home/Client', "'" + deviceId  + "'");
    } 
})();

ok, so I couldn't get to the bottom of adding an onClick function dynamically, so, I changed my code around, and pass a viewModel array containing all the data I needed to create the table, so when I open the clients view, I send a list of all the relevant clients, and when I select the row I need, I just pass a single viewmodel of the client, I then update the html with the c# code @model.variable. 好的,所以我无法深入地动态添加onClick函数,因此,我改变了代码,并传递了一个ViewModel数组,其中包含创建表所需的所有数据,因此当我打开客户端视图时,我发送所有相关客户端的列表,当我选择所需的行时,我只传递了客户端的单个viewmodel,然后使用c#代码@ model.variable更新html。 I can easily update these from my signalr code once its created. 创建信号器代码后,我可以轻松地从它们中更新这些代码。

thanks for everyones input. 感谢大家的投入。

Just to update, I have found a solution I think, I could create the element, add the data to the innerHtml, and use: 为了更新,我找到了一个解决方案,我可以创建元素,将数据添加到innerHtml并使用:

setAttribute("onclick", "navigateToController('/Home/Client', '" + deviceId + "')"); setAttribute(“ onclick”,“ navigateToController('/ Home / Client','” + deviceId +“')”);

I have used this in another part of my solution, and it works... 我在解决方案的另一部分中使用了它,并且可以正常工作...

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

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