简体   繁体   English

验证表格行中动态添加的输入类型

[英]validate dynamically added input types in a row of table

I am trying to validate the dynamically added input type field which are in a row of a table on button click event. 我正在尝试验证按钮单击事件中表的一行中动态添加的输入类型字段。 The very first input type field has an ID so I am able to put a validation on it. 第一个输入类型字段具有ID,因此我可以对其进行验证。 But when I add more rows in the table dynamically , I don't know how to validate those input types of rows. 但是,当我在表中动态添加更多行时,我不知道如何验证那些输入类型的行。

HTML : HTML:

<table class="table table-bordered table-hover" id="tab_logic">
                <thead>
                    <tr>

                        <th class="text-center">Channel Group Name
                        </th>
                        <th class="text-center">Description
                        </th>

                    </tr>
                </thead>
                <tbody>
                    <tr id='addr0'>

                        <td id="CGN">
                            <input type="text" id="channelgrpName" name='channelgrpName' placeholder='Channel Group Name' class="form-control" />
                        </td>
                        <td id="DES">
                            <input type="text" id="description" name='description' placeholder='Description' class="form-control" />
                        </td>

                    </tr>
                    <tr id='addr1'></tr>
                </tbody>
            </table>

Jquery : jQuery的:

$(document).ready(function () {

    var i = 1;
    $("#add_row").click(function () {

        $('#addr' + i).html("<td><input name='name" + i + "' type='text' placeholder='Channel Group Name' class='form-control' style='width:50%'  /> </td><td><input  name='mail" + i + "' type='text' placeholder='Description'  class='form-control' style='width:50%'></td>");

        $('#tab_logic').append('<tr id="addr' + (i + 1) + '"></tr>');
        i++;
    });
    $("#delete_row").click(function () {
        if (i > 1) {
            $("#addr" + (i - 1)).html('');
            i--;
        }
    });
});

$('#btnSave').click(function () {

 // what should i to do here to validate dynamically added input types in row

});

Thanks in advance. 提前致谢。

Just try this, if you need only check blanks 如果只需要检查空白,请尝试此操作

Like this: 像这样:

$('#btnSave').click(function () {
  var validate = true;
    $('#tab_logic').find('tr input[type=text]').each(function(){
    if($(this).val() == ""){
        validate = false;
    }
  });
  if(validate){
    alert("Form is validate")
    return true;// you can submit form or send ajax or whatever you want here
  } else {
    alert("please fill all the details")
    return false;

  }
});

Js Fiddle for check blank fileds Js Fiddle检查空白文件

And if you need more validation just use some js plugins to validate like 如果您需要更多验证,请使用一些js插件进行验证,例如

jQuery-Validation-Engine jQuery-Validation-Engine

please check this link i hope this will helpful for you. 

https://jsfiddle.net/ffgbvsg0/

You don't need ids at all. 您根本不需要ID。

In your first row and when you append new rows, give them a class for eg: added-row . 在第一行中,当您追加新行时,请为它们提供一个类,例如: added-row

And bind the click listener on #tab_logic tbody tr.added-row 并将点击侦听器绑定到#tab_logic tbody tr.added-row

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

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