微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

使用foreach asp.net核心内的multiplus按钮获取值

如何解决使用foreach asp.net核心内的multiplus按钮获取值

我在foreach中有multiplus按钮,当我单击它时,需要将他的data-value提交/发送到操作/方法。我该怎么办?

  <form asp-area="App" id="formMenu">
     @foreach (var item in ViewBag.Atendimento)
     {
             @* I was doing with inputs,but with more than one button,I can't do it anymore
             <input value="@item.IdProduto" name="id" type="hidden" />
             <input value="@item.IdProdutoMeia" name="idMeio" id="idMeio" type="hidden" />
             <input value="@item.Quantidade" name="Qtd" type="hidden" />

             <button class="btn btn-info btn-sm" id="btnEdit">
                 Edit
             </button>
             *@

             @* So,I started trying like this *@
             <button class="btn btn-outline-info btn-sm" onClick="btnEdit(this)"  data-id="@item.IdProduto" data-idmeio="@item.IdProdutoMeia" data-qtd="@item.Quantidade">
                <i class="fas fa-pen fa-sm"></i>
             </button>
     }
 </form>


 <script type="text/javascript">

       // With just one button,like I sad,it was working fine like this
       $("#btnEdit").click(function () {
            if ($("#idMeio").val() == '') {
                $("#formMenu").attr("action","@Url.Action("Edit","Produto")");
            } else {
                $("#formMenu").attr("action","@Url.Action("EditMeio","Produto")");
            };
            $("#formMenu").submit();
        });


     // So I have been traying this way,but don't kNow how to do
     function btnEdit(event) {
            var button = $(event.relatedTarget)
            var idItem = button.attr('data-id')
            var idItemmeio = button.attr('data-idmeio')
            var qtdItem = button.attr('data-qtd')

           ???
    };
    </script>

请提供一些想法,代码等帮助

解决方法

如果有多个按钮,并且所有按钮都带有标签<button>,则应用带有按钮标签的点击功能并获取按钮ID。根据按钮ID,可以将相关URL设置为调用Action方法。

$("button").click(function () {
            alert(this.id);
            var btnId = this.id
        });

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。