[英]Get id of dynamically generated button in Razor
I've a table with a list of object and in every row I've dynamically generated button to delete the row. 我有一个带有对象列表的表,并且在每行中我都动态生成了一个删除行的按钮。
how to get in @{ c# }
the id of the pressed button? 如何获取
@{ c# }
所按下按钮的ID?
@for (int i = 0; i < BL.SNG.GetListLen(); i++)
{
<form method="post"
action="">
<input type="submit"
value="Delete @i"
id="@i"
onclick="button_click(this.id)" />
</form>
</td>
You are placing the id on the wrong element. 您将ID放在错误的元素上。 Since the loop creates a new form element and a new button on each iteration.
由于循环在每次迭代时都会创建一个新的表单元素和一个新的按钮。 the id should be assigned to the form element itself.
id应该分配给form元素本身。 I assume button_click refers to some JavaScript function defined elsewhere.
我假设button_click引用了其他地方定义的一些JavaScript函数。
Note that it is discouraged to attach event handlers to elements in the html markup portion of your page. 请注意,不建议将事件处理程序附加到页面html标记部分中的元素。 You should generally add them from JavaScript.
通常,您应该从JavaScript添加它们。 Like this:
像这样:
document.getElementsByTagName("input")
.forEach( addEventListener("click", function (...) { });
Sorry, I just noticed your comment about deleting the row represented from the database. 抱歉,我刚刚注意到您有关删除数据库中代表的行的评论。 You could use the submit action of the form itself to do that as well.
您也可以使用表单本身的Submit操作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.