我的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>
和保存按钮:
<button class="btn " type="submit" form="controller" name="save" value="save" id="save" onclick="return onSave()">Save</button>
每行基本都是空的,我想要做的是,点击保存按钮时只提交:
尽可能使用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;
};
上面的代码会检查textarea
id comments
是否发生了更改,如果其为空,则会将isFilled
设置为false
,因此提交将失败。我所做的代码问题是,当我使用textarea
更改了两个id of comments
时,它只会检查上次更改的textarea
。
知道如何解决这个问题吗?
答案 0 :(得分:1)
尝试
//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
}