【问题标题】:Assign ID to an asp.net Javascript button将 ID 分配给 asp.net Javascript 按钮
【发布时间】:2021-04-20 08:57:27
【问题描述】:

在我的代码中,我有一个用于输入数据的按钮,但如下图所示,它只读取与按钮无关的第一行。

我相信这是因为按钮 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>
         }

谢谢

【问题讨论】:

标签: javascript html jquery asp.net asp.net-mvc


【解决方案1】:

当您单击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
})

【讨论】:

  • 对了,谢谢,顺便说一句,我正在接收数据,你能帮我保存吗?因为我使用 db.savechanges 但它没有记录
  • 嗨,我会帮忙的,但是我对asp的了解有限:)
猜你喜欢
  • 2011-06-23
  • 2015-09-12
  • 1970-01-01
  • 2015-06-01
  • 1970-01-01
  • 2014-01-21
  • 2016-11-30
  • 2014-03-29
  • 1970-01-01
相关资源
最近更新 更多