Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 检查表上的迭代行(如果为空)_Javascript_Jquery - Fatal编程技术网

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']
这将只选中一个id
comments
的文本框。对不同的元素使用不同的
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
}