繁体   English   中英

遍历多种形式并在jquery中获取其输入值

[英]Loop through multiple form and get their input values in jquery

我正在使用jquery动态生成表单。 如何遍历所有表格并提醒其输入值。

生成表单的代码

 $.ajax({ method: 'GET', url: 'api/PayrollSystem/GetEmployeeSalaryAmount?AdminId=' + 1, success: function(data) { if (Object.keys(data).length == 0) { $.alert({ title: 'Saved', content: 'Account Number Saved!', }); var row = ('<tr ><td colspan="3" text-align="center"> <b>No Registered Users</b></td></tr>'); $('.EmpTable').append(row); } else { $.each(data, function(index, value) { var rows = ('<div id="form_div"><form method="post"><table class="table table-hover employee_table' + index + ' " align=center><thead><tr><td><b>Employee</td> <td><b>Amount</td></tr></thead><tbody><tr><td><b>' + value.FullName + '</b></td><td><input type="hidden" class="form-control Leavename" name="Leavename[]" value="Basic" placeholder="Enter Leave Name"> <input readonly class="form-control LeaveAmount" type="number" name="LeaveAmount[]" value=' + value.SalaryAmount + ' /><input class="form-control EmpId" type="hidden" name="Id[]" value=' + value.Id + ' /></td></tr><tr id="row' + index + 1 + '"><td><input type="text" class="form-control Leavename" name="Leavename[]" placeholder="Enter Leave Name"> </td><td> <input type="number" class="form-control LeaveAmount" name="LeaveAmount[]" placeholder="Enter Leave Amount"></td> </tr></tbody> </table> <input type="button" class="btn btn-success addRow" onclick="add_row(' + index + '); " value=" + Add More Payroll Item"></form> </div><hr />'); $('.EmpTable').append(rows); }); } } }); 

我在哪里尝试通过ID获取输入字段的值

 $('#saveAdj').click(function() { $('form').each(function() { alert($('.EmpId').val()); }); }); 

$('#saveAdj').click(function() {
  $('form').each(function() {
    var thisForm = this;
    alert($('.EmpId', thisForm).val());
  });
});

$('selector', container)将帮助您仅从容器中搜索;

您没有为在哪里找到.EmpId提供任何上下文。

传递给each()方法的回调函数将自动传递值,以访问要枚举的集合。 如果使用这些参数值,则可以通过将其作为第二个参数传递给JQuery对象来提供该上下文。

$('#saveAdj').click(function() {
  // JQuery's .each() method will automatically pass 3 arguments
  // to the provided callback function:
  //  a reference to the index position of that element
  //  a reference to the element being enumerated at the moment
  $('form').each(function(index, element) {

    // The element argument can be passed as the second argument to 
    // the JQuery object (after the selector) to provide context for where to search
    alert($('.EmpId', element).val());
  });
});

暂无
暂无

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

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