由javascript生成时,输入必需属性不起作用

时间:2016-08-13 07:16:22

标签: javascript jquery html twitter-bootstrap

输入必需属性在直接写在Body上时有效。生成检查时显示的新行必需属性但不适用于下一行元素。我试图说明这种情况。

对于以下图像,必填字段正在运行 Required field Working only for the first Row

但是当我按下Add按钮时,Next Rows Element Required Attribute不能像下面这样工作: For Next Any other Rows Element Required Attribute is not Working

以下是我的代码。



    <!DOCTYPE html>
<html>
<head>
	<title>Test by JavaScript</title>
	<link type="text/css" href="http://netdna.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet" />
    <script type="text/javascript" src="https://code.jquery.com/jquery-1.10.2.min.js"></script>
    <script type="text/javascript" src="http://netdna.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
    <script type="text/javascript" src="http://1000hz.github.io/bootstrap-validator/dist/validator.min.js"></script>
</head>
<form id="testForm" name="testForm" method="post" action="" autocomplete="off" enctype="multipart/form-data" data-toggle="validator" role="form">
                <table id="testDataTable" class="table">
                  <thead>
                    <tr>
                      <th><div class="form-group">Y/N</div></th>
                      <th><div class="form-group">Name</div></th>
                      <th><div class="form-group">No</div></th>
                      <th><div class="form-group">Value</div></th>
                    </tr>
                  </thead>
                  <tbody id="testTbody">
                    <tr>
                      <td><div class="form-group"><INPUT type="checkbox" id="t_chk" name="t_chk" /></div></td>
                      <td><div class="form-group"><input type="text" class="form-control" id="t_NameTxt" name="t_NameTxt[]" required /></div></td>
                      <td><div class="form-group"><input type="text" class="form-control" id="t_IDTxt" name="t_IDTxt[]" required /></div></td>
                      <td><div class="form-group"><input type="text" class="form-control" id="t_ApxValTxt" name="t_ApxValTxt[]" required /></div></td>
                    </tr>
                  </tbody>
                </table>
                <div id="testBtnBlk" class="col-md-10">
                  <div class="form-group">
                    <input type="button" value="Add" class="btn btn-warning" onclick="addRowTest('testDataTable')"/>
                    <input type="button" id="delRow" class="btn btn-danger" value="Delete"/>
                  </div>
                </div>
                <script type="text/javascript">
                  function addRowTest(tableID) {
                    var table = document.getElementById(tableID);
                    var rowCount = table.rows.length;
                    var row = table.insertRow(rowCount);
                    var newcell = '<tr><td><div class="form-group"><INPUT type="checkbox" id="t_chk'+rowCount+'" name="t_chk"/></div></td><td><div class="form-group"><input type="text" class="form-control" id="t_NameTxt'+rowCount+'" name="t_NameTxt[]" required /></div></td><td><div class="form-group"><input type="text" class="form-control" id="t_IDTxt'+rowCount+'" name="t_IDTxt[]" required /></div></td><td><div class="form-group"><input type="text" class="form-control" id="t_ApxValTxt'+rowCount+'" name="t_ApxValTxt[]" required /></div></td></tr>';
                    table.rows[rowCount].innerHTML = newcell;
                  }
                  $(document).ready(function () {
                    $("#delRow").on("click", function () {
                      if($('#testTbody').children('tr').length>1){
                        $('table tr').has('input[name="t_chk"]:checked').remove()
                    }
                    })
                  });
                </script>
                </form>
</body>
</html>
&#13;
&#13;
&#13;

我的超文本中有人可以帮助解决错误吗?

1 个答案:

答案 0 :(得分:1)

添加新行后,您需要重新初始化插件。这与HTML5 required属性无关。

只需检查DOC,您需要在添加功能的底部使用:

$('#testForm').validator('update');

<!DOCTYPE html>
<html>

<head>
  <title>Test by JavaScript</title>
  <link type="text/css" href="http://netdna.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet" />
  <script type="text/javascript" src="https://code.jquery.com/jquery-1.10.2.min.js"></script>
  <script type="text/javascript" src="http://netdna.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
  <script type="text/javascript" src="http://1000hz.github.io/bootstrap-validator/dist/validator.min.js"></script>
</head>
<form id="testForm" name="testForm" method="post" action="" autocomplete="off" enctype="multipart/form-data" data-toggle="validator" role="form">
  <table id="testDataTable" class="table">
    <thead>
      <tr>
        <th>
          <div class="form-group">Y/N</div>
        </th>
        <th>
          <div class="form-group">Name</div>
        </th>
        <th>
          <div class="form-group">No</div>
        </th>
        <th>
          <div class="form-group">Value</div>
        </th>
      </tr>
    </thead>
    <tbody id="testTbody">
      <tr>
        <td>
          <div class="form-group">
            <INPUT type="checkbox" id="t_chk" name="t_chk" />
          </div>
        </td>
        <td>
          <div class="form-group">
            <input type="text" class="form-control" id="t_NameTxt" name="t_NameTxt[]" required />
          </div>
        </td>
        <td>
          <div class="form-group">
            <input type="text" class="form-control" id="t_IDTxt" name="t_IDTxt[]" required />
          </div>
        </td>
        <td>
          <div class="form-group">
            <input type="text" class="form-control" id="t_ApxValTxt" name="t_ApxValTxt[]" required />
          </div>
        </td>
      </tr>
    </tbody>
  </table>
  <div id="testBtnBlk" class="col-md-10">
    <div class="form-group">
      <input type="button" value="Add" class="btn btn-warning" onclick="addRowTest('testDataTable')" />
      <input type="button" id="delRow" class="btn btn-danger" value="Delete" />
    </div>
  </div>
  <script type="text/javascript">
    function addRowTest(tableID) {
      var table = document.getElementById(tableID);
      var rowCount = table.rows.length;
      var row = table.insertRow(rowCount);
      var newcell = '<tr><td><div class="form-group"><INPUT type="checkbox" id="t_chk' + rowCount + '" name="t_chk"/></div></td><td><div class="form-group"><input type="text" class="form-control" id="t_NameTxt' + rowCount + '" name="t_NameTxt[]" required /></div></td><td><div class="form-group"><input type="text" class="form-control" id="t_IDTxt' + rowCount + '" name="t_IDTxt[]" required /></div></td><td><div class="form-group"><input type="text" class="form-control" id="t_ApxValTxt' + rowCount + '" name="t_ApxValTxt[]" required /></div></td></tr>';
      table.rows[rowCount].innerHTML = newcell;
      $('#testForm').validator('update');
    }
    $(document).ready(function() {
      $("#delRow").on("click", function() {
        if ($('#testTbody').children('tr').length > 1) {
          $('table tr').has('input[name="t_chk"]:checked').remove()
        }
      })
    });
  </script>
</form>
</body>

</html>