Javascript 检查表上的迭代行(如果为空)
我的Javascript 检查表上的迭代行(如果为空),javascript,jquery,Javascript,Jquery,我的JSP <c:forEach var="test" value="${test}"> <table> <tr> <td><textarea class="no-resize" id="comments"></textarea></td> <td><select class="select"> ... </select> </td
JSP
<c:forEach var="test" value="${test}">
<table>
<tr>
<td><textarea class="no-resize" id="comments"></textarea></td>
<td><select class="select"> ... </select> </td>
<td><textarea class="no-resize" id="reason"></textarea></td>
<td><textarea class="no-resize" id="description"></textarea></td>
</tr>
</table>
</c:forEach>
上面的代码检查带有id注释的textarea
是否更改,如果为空,则将isFilled
设置为false
,因此提交将失败。我所做的代码的问题是,当我用注释id更改两个textarea
时,它只会检查最后更改的textarea
你知道怎么解决这个问题吗?试试看
//this is a flag which will be set to false if an invalid row is found
var valid = true;
//iterate through each row of the table, it is because we need to validate the textarea's in each row
$('tr').each(function(){
//all textarea's in the row
var els = $(this).find('textarea');
//we uses filter to find the textarea's where the length of the textarea is 0
var len = els.filter(function(){
return $.trim($(this).value()).length == 0;
});
//if the textarea's length is neither 0 or not equals to the total length then it is not valie
if(len > 0 && len != els.length){
valid = false;
return false;
}
});
if(!valid){
//there are incomplete records
}
页面上有多个元素具有相同的id
是无效的HTML。。不调整[id='comments']
这将只选中一个idcomments
的文本框。对不同的元素使用不同的id
。@ArtyomNeustroev好的,我将删除该id。@Unknown我只能用jstl(id=“comments-${var}”类似这样的东西)生成不同的id
,我不知道如何才能获得jquery的想法。你能解释一下你的代码吗?实际上,我是jQuery新手,我不知道如何将您的代码集成到我的代码中。
var isFilled = true;
$(".no-resize[id='comments']").each(function(){
$(this).change(function(){
if(!$(this).val() == ''){
setTextBox(false);
}else{
setTextBox(true);
}
});
});
function setTextBox(isEmpty)
{
isFilled = isEmpty;
}
function onSave()
{
return isFilled;
};
//this is a flag which will be set to false if an invalid row is found
var valid = true;
//iterate through each row of the table, it is because we need to validate the textarea's in each row
$('tr').each(function(){
//all textarea's in the row
var els = $(this).find('textarea');
//we uses filter to find the textarea's where the length of the textarea is 0
var len = els.filter(function(){
return $.trim($(this).value()).length == 0;
});
//if the textarea's length is neither 0 or not equals to the total length then it is not valie
if(len > 0 && len != els.length){
valid = false;
return false;
}
});
if(!valid){
//there are incomplete records
}