[英]Javascript/jquery Get Row Id on button click in a table
I am beginner in javascript.So i am trying to get selected row id on button click.I am not sure i tried examples on internet however could not run code. 我是javascript的新手,所以我尝试在按钮click上获取选定的行ID。我不确定我是否在Internet上尝试过示例,但无法运行代码。
<tr>
<input type="hidden" name="InterestID" id="InterestID" value="@info.InterestID" />
<th>
<input data-required="true" class="input-sm form-control datepicker" size="16" type="text" name="ValidDate" id="ValidDate" value="@info.ValidDate.ToShortDateString()" />
</th>
<th>
<input class="form-control" data-required="true" name="Rate" id="Rate" value="@info.Rate" />
</th>
<th>
<button type="button" class="btn btn-sm btn-danger" onclick="DeleteInterest()">Delete</button>
</th>
</tr>
function DeleteInterest(parameters) {
alert("Delete Button clicked");
}
When i click to "Delete" button , i want to get @info.InterestId for selected row.So how can i get selected row id on "Delete" button click by using javascript/jquery ? 当我单击“删除”按钮时 ,我想获取所选行的@ info.InterestId 。那么如何使用javascript / jquery在“删除”按钮上单击以获取所选行ID?
Any help will be appreciated. 任何帮助将不胜感激。
Thanks. 谢谢。
You can try to pass @info.InterestID to DeleteInterest: 您可以尝试将@ info.InterestID传递给DeleteInterest:
Html: HTML:
<button type="button" class="btn btn-sm btn-danger" onclick="DeleteInterest(@info.InterestID)">Delete</button>
And Javascript 和Javascript
function DeleteInterest(id) {
alert("Delete Button clicked with id " + id);
}
Hope that helps! 希望有帮助!
Use as below, As Ruben said if you not passing id into function then you can not get directly. 如Ruben所说,按如下所述使用,如果不将id传递给函数,则无法直接获取。
<button type="button" class="btn btn-sm btn-danger"
onclick="DeleteInterest('@info.InterestID')">Delete</button>
function DeleteInterest(id) {
alert("Delete Button clicked with id " + id);
}
OR You can get as below, without change HTML
或者您可以得到如下内容,无需更改
HTML
function DeleteInterest() {
alert("Delete Button clicked with id " + $("#InterestID").val());
}
I would get the value dynamically: 我将动态获取值:
var DeleteInterest = function(e) {
alert(this.parentNode.parentNode.getElementsByClassName("value")[0].value);
}
And the HTML: 和HTML:
<tr>
<input type="hidden" class="value" name="InterestID" id="InterestID" value="@info.InterestID" />
<th>
<input data-required="true" class="input-sm form-control datepicker" size="16" type="text" name="ValidDate" id="ValidDate" value="@info.ValidDate.ToShortDateString()" />
</th>
<th>
<input class="form-control" data-required="true" name="Rate" id="Rate" value="@info.Rate" />
</th>
<th>
<button type="button" class="btn btn-sm btn-danger" onclick="DeleteInterest(this)">Delete</button>
</th>
</tr>
I even consider more interesting having a hashmap in js matching the button with the value element instead of hidding it in the html, then, you should have: 我什至认为在js中将按钮与value元素匹配而不是将其隐藏在html中的哈希表中而不是将其隐藏在html中更为有趣,那么,您应该具有:
var list = {<button_html_node_element>: '@info.InterestID'}
And then, your delete function: 然后,您的删除功能:
var DeleteInterest = function(e) {
alert(list[e]);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.