gpt4 book ai didi

javascript - 由 javascript 生成时所需的输入属性不起作用

转载 作者:行者123 更新时间:2023-11-27 22:38:22 24 4
gpt4 key购买 nike

Input required 属性直接写在 Body 上时才起作用。生成新行时,检查时显示所需属性,但不适用于下一行元素。我正在尝试说明这个场景。

对于下图,必填字段正在工作 Required field Working only for the first Row

但是当我按下“添加”按钮时,“下一行元素必需属性”不起作用,如下所示: 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>

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

最佳答案

添加新行后,您需要重新初始化插件。这与 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>

关于javascript - 由 javascript 生成时所需的输入属性不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38930257/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com