[英]Bootstrap form JS validation in loop
What is the correct way to get an form validation when there is an in a loop? 在循环中进行表单验证的正确方法是什么? ($i set somewhere else)
($ i设置在其他位置)
<form class="form-calculator" id="edit'.$i.'" action="includes/processing/manual_edit_processing.php" method="post">
<div class="modal-body">
<div class="form-group has-feedback" id="div_edit_naam['.$i.']">
<input type="text" class="form-control" id="edit_naam['.$i.']" name="edit_naam['.$i.']" placeholder="Naam" value="'.$row_list['naam'].'" onkeyup="validate_edit(this, '.$i.'))" onmousemove="validate_edit(this, '.$i.')"><span class="glyphicon glyphicon-warning-sign form-control-feedback" id="edit_naam_status['.$i.']">
</div>
</div>
</div>
</div>
</div>
</form>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal" onclick="window.location.reload()">Sluiten</button>
<button type="button" id="submit_edit'.$i.'" alt="'.$i.'" class="btn btn-primary edit">Bewerk</button>
</div>
Above is my HTML part, below my JS part, I think I have messed up with the ID's unique? 上面是我的HTML部分,下面是我的JS部分,我想我已经搞砸了ID的唯一性吗?
<script type="text/javascript">
window.onload=function() { document.getElementById('submit_edit[]')[nr].disabled = true; }
function validate_edit(selectVeld, nr)
{
// edit_naam
if(document.getElementById('edit_naam[]')[nr].value.length < 5) { document.getElementById('div_edit_naam[]')[nr].className = "form-group has-warning has-feedback"; document.getElementById('edit_naam_status[]')[nr].className = "glyphicon glyphicon-warning-sign form-control-feedback"; }
else
{ document.getElementById('div_edit_naam[]')[nr].className = "form-group has-success has-feedback"; document.getElementById('edit_naam_status[]')[nr].className = "glyphicon glyphicon-ok form-control-feedback"; }
// Submit form
if(document.getElementsByClassName('has-error').length > 0 || document.getElementsByClassName('has-warning').length > 0) { document.getElementById('submit_edit').disabled = true; }
else { document.getElementById('submit_edit').disabled = false; }
}
</script>
You should use: 您应该使用:
document.getElementById("edit_naam["+nr+"]")
Instead of: 代替:
document.getElementById('edit_naam[]')[nr]
Most of the ID you set to the HTML elements have this format something[index]
so that is the construct you should pass as a parameter to the document.getElementById
function. 您为HTML元素设置的大多数ID的格式均为
something[index]
因此应将其作为参数传递给document.getElementById
函数。 document.getElementById('edit_naam[]')[nr]
doesn't make any sense since this function returns only one HTML element. document.getElementById('edit_naam[]')[nr]
没有任何意义,因为此函数仅返回一个HTML元素。
Also, [
and ]
are not accepted characters for IDs in HTML 4. 此外,
[
和]
也不是HTML 4中ID的可接受字符。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.