简体   繁体   English

如何使用 Jquery 将 append 输入类型提交到表上并绑定它?

[英]How to append input type submit elements on table using Jquery and bind it?

I am trying to append a table row which has a form and input type submit.我正在尝试 append 一个表格行,它有一个表单和输入类型提交。 Typically, this is what my loop looks like:通常,这就是我的循环的样子:

@foreach (var item in Model)
   {
     using (Html.BeginForm("Edit", "User"))
          {
              <tr>
                  <input type="hidden" name="Id" value="@item.Id" />
                  <td>
                    @Html.DisplayFor(modelItem => item.Email)
                    @Html.HiddenFor(modelItem => item.Email, new { name = "Email" })
                  </td>
                  
                     <a href="@Url.Action("Edit", new { Id = item.Id })">Edit</a>
                      <button type="submit" onclick="return confirm('Disable this?');" name="btnDisable" id="btnDisable">Disable</button>
                  </td>
             </tr>
          }
    }

Now on my AJAX call, I need to add additional user here on this table row, here's how I usually do it:现在在我的 AJAX 调用中,我需要在此表行上添加其他用户,这是我通常的做法:

 $.ajax({
        type: "POST",
        url: 'URL',
        dataType: 'json'
       }).done(function (data) {
        if (data.status == 'True') {
            var usrObj = data.uobj;

            $(function () {
            var table = $('#tblUsers tbody');

            for (var i = 0; i < usrObj.length; i++) {
                $('#tblUsers').find('tbody')
                    .append('<tr><form action="/User/Edit" method="post">' +
                     '<input type="hidden" name="Id" value=' + usrObj[i].Id + '>' +
                     '<td>' + usrObj[i].Email + '</td>' +
                     "<td><a href='" + "/User/Edit/" + usrObj[i].Id + "'" + "></i>Edit</a>" + 
                     `<button type='submit' name='btnDisable' id = 'btnDisable' onclick="return confirm('Disable this?')";>Disable</button></td>` +
                        '</form></tr>');
                }
            }                
    });

Now what is happening is that, the appended link is working fine since everything the link that is needed to function is already set on it's attributes:现在发生的事情是,附加的链接工作正常,因为 function 所需的链接已经设置在它的属性上:

<a href='" + "/UserM/Edit/" + usrObj[i].Id + "'" + "></i>Edit</a>

This is I am having a hard time submitting the form:这是我很难提交表格:

<button type='submit' name='btnDisable' id = 'btnDisable' onclick="return confirm('Disable this?')";>Disable</button>

I tried adding the following code below but still, nothing is happening, the confirm dialog box is popping up, but upon clicking Ok button, nothing is happening.我尝试在下面添加以下代码,但仍然没有发生任何事情,弹出确认对话框,但是单击“确定”按钮后,什么也没有发生。 The form is not submitting.表单未提交。 But the other rows are working fine after the append.但是在 append 之后其他行工作正常。

Here's what I tried, but all of these are not working fine.这是我尝试过的,但所有这些都不能正常工作。

  $(function () {
      $(document).on('submit', 'form', function (event) {
             $(this).submit();
          });
   });          

I guess the way I appended my table row is the problem but I am not clearly sure.我想我附加表格行的方式是问题,但我不太确定。

Problem in your current jquery code was that when you were appending new rows inside table it was going outside tr tag that's the reason form submit was not working.当前 jquery 代码中的问题是,当您在表中附加新行时,它超出了 tr 标签,这是表单提交不起作用的原因。 Instead you can put form tag inside td tag and the input field as well which you need to pass to backend so that form gets trigger using button inside it.相反,您可以将表单标记放在td标记和需要传递给后端的输入字段中,以便使用其中的按钮触发表单。

Demo Code :演示代码

 //this is for demo:) var usrObj = [{ "Email": "cdcdc", "Id": 11 }, { "Email": "cdcdc2", "Id": 12 }] for (var i = 0; i < usrObj.length; i++) { $('#tblUsers').find('tbody').append('<tr><td>' + usrObj[i].Email + '</td>' + "<td><form action='/User/Edit' method='post'><a href='" + "/User/Edit/" + usrObj[i].Id + "'" + "></i>Edit</a>" + `<button type='submit' name='btnDisable' onclick="return confirm('Disable this?')";>Disable</button>` + '<input type="hidden" name="Id" value=' + usrObj[i].Id + '/></form>' + "</td></tr>"); }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <table id="tblUsers"> <tbody> <form> <tr> <input type="hidden" name="Id" value="1" /> <td> frg </td> <td> <a href="@Url.Action(" Edit ", new { Id = item.Id })">Edit</a> <button type="submit" onclick="return confirm('Disable this?');" name="btnDisable" id="btnDisable">Disable</button> </td> </tr> </form> <form> <tr> <input type="hidden" name="Id" value="2" /> <td> ckwlkev </td> <td> <a href="@Url.Action(" Edit ", new { Id = item.Id })">Edit</a> <button type="submit" onclick="return confirm('Disable this?');" name="btnDisable" id="btnDisable">Disable</button> </td> </tr> </form> </tbody> </table>

Please update your ajax code with请更新您的 ajax 代码

for (var i = 0; i < usrObj.length; i++) {    
    $('#tblUsers').find('tbody').append('<tr><form action="/User/Edit" method="post">' +
    '<input type="hidden" name="Id" value=' + usrObj[i].Id + '>' +
    '<td>' + usrObj[i].Email + '</td>' +
    '<td><a href="' + '/User/Edit/' + usrObj[i].Id + '"></i>Edit</a>' + 
    '<button type="submit" name="btnDisable" id = "btnDisable" onclick="return confirm(\'Disable this?\')";>Disable</button></td>' +
    '</form></tr>');
} 

Also, $(document).on has much lower performance than $('form.remember').on('submit') .此外, $(document).on的性能远低于$('form.remember').on('submit') It now has to listen for all submit events in the document.它现在必须监听文档中的所有提交事件。 It's much better to restrict the scope a little than listen on document, if possible如果可能的话,限制 scope 比听文件要好得多

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM