问题描述
在我的代码中,我有一个用于输入数据的按钮,但如下图所示,它只读取独立于按钮的第一行。
我相信这是因为按钮 id 没有分配给每一行。
如何从每一行获取我的 id 到我的按钮?
我的 ID 是 ID_Info
<td>
<button class="btn ToEditbtn" ><span class="fa fa-plus-square" aria-hidden="true"></span>Edit</button>
<button class="btn Editbtn" style="display:none"><span class="fa fa-plus-square-o" aria-hidden="true">Save</span></button>
</td>
@section scripts{
<script>
$(function () {
$("#ordertable").on("click",".ToEditbtn",function () {
$(this).hide();
var currenttr = $(this).closest("tr");
currenttr.find(".Editbtn").show();
currenttr.find(".Status").prop("disabled",false);
currenttr.find(".Obs").prop("disabled",false);
});
//update
$("#ordertable").on("click",".Editbtn",function () {
var status = new Object();
status.ID_Info = $(".ID_Info").val();
status.Status = $(".Status").val();
status.Obs = $(".Obs").val();
...
<script>
}
谢谢
解决方法
当您点击 save
按钮时,您将使用 $(".ID_Info")
获取值,这不会为您提供所需的值,因为有多个具有相同名称的类。因此,要仅定位所需的值,您可以使用 $(this).closest('tr').find('valueyouneedtofind')..
即:
$("#ordertable").on("click",".Editbtn",function() {
var status = new Object();
status.ID_Info = $(this).closest('tr').find(".ID_Info").val();
status.Status = $(this).closest('tr').find(".Status").val();
status.Obs = $(this).closest('tr').find(".Obs").val();
//other codes
})