2017-12-12 2 views
1

私はjQueryのテーブル内のすべての入力テキストボックスが空であることを確認するにはどうすればよいですか?

$('#library_info_tbl tbody').on("keyup", "tr td input:nth-child(2)", function() { 
    if(all the textboxes are empty) 
    // do something 
}); 

<table> 
    <tr> 
    <td>abc</td> 
    <td>781</td> 
    <td><input id="barcodedb0" class="hide" type="text" /><input id="barcode0" class="hide" type="text" /></td> 
    </tr> 
    <tr> 
    <td>abc</td> 
    <td>781</td> 
    <td><input id="barcodedb0" class="hide" type="text" /><input id="barcode0" class="hide" type="text" /></td> 
    </tr> 
</table> 

のようなデータを持つHTMLテーブルを持っている私は、テーブル内のすべてのテキストボックスが空であることを確認する方法を見つけるためにしようとしています。

「hide」クラスのテキストボックスは非表示であり、他の目的に使用されています。

助けてください!

+4

IDは同じにすることはできません気をループ反復のためでそれを行うと、それぞれの値を確認することができます。 –

+0

空の入力をクラス '' 'hide'''でも見つけたいですか? IDも同じではありません。 –

+0

すべての入力を取得する必要があります。一度それらを持っていれば、あなたの必要性を満たさないすべての入力をループオーバーして除外します。 – Rajesh

答えて

1

あなたはこのコードだけ第二の入力のために動作します。この を使用することができます。

$('#library_info_tbl tbody').on("keyup", "tr td input:nth-child(2)", function() { 
     var oneEmpty = false; 
     $("#library_info_tbl tbody tr td input:nth-child(2)").filter(function() { 
       return $.trim($(this).val()) == ''; 
     }); 
     if(oneEmpty) 
     // do something 
}); 

このコードは、すべての入力に対してのみ動作します。

$('#library_info_tbl tbody').on("keyup", "tr td input:nth-child(2)", function() { 

     if($("#library_info_tbl tbody input:empty").length == $("#library_info_tbl tbody input").length) 
     { 
     // do something 
     } 
}); 

// Instead of $("#library_info_tbl tbody input:empty") you can also use $("input.hide:empty") 
1

あなたはセレクタとしてinput[type=text]を使用することができ、この

var allempty = true; 
$('.hide').each(function(){ 
     if($(this).val()!=''){ 
      allempty = false; 
     }else{ 
      allempty = true; 
     } 
    }); 

if(allempty) { 
    //this is empty 
} 
+0

それはテーブル内の他のすべてのテキストボックスで動作します... –

+0

それはすべての "非表示"クラスのために動作します、それが動作したい場合$( '。otherclass')を$( '。hide')に置き換えます。 –

1

をお試しください:

IE用:あなたのテーブルのIDを考慮

var emptyValues = 0; 
$('table input[type=text]').each(function(){ 
    if($(this).val() != ''){ 
     console.log($(this).val()); 
     emptyValues = 1; 
    } 
}); 
if(emptyValues == 0){ 
    //enter code here 
} 
1

tableIdで、あなたが試みることができる:

if($("table#tableId input[class!='hide'][value='']").length == $("table#tableId input[class!='hide']").length) { 
// do something 
} 

または単に:

if($("table input[class!='hide'][value='']").length == $("table input[class!='hide']").length) { 
// do something 
} 
+0

空白スペースでも動作しますか? – OIIO

+0

いいえ、 '' 'は空を意味するわけではないのですか?私はあなたがトリムと組み合わせてセレクタを使用することはできないと思う。空白をチェックする必要がある場合は、別の方法が必要です。 – SmartDev

1

あなたは

function checkEmpty(){ 
 
    var elems = $("table input[type=text]"); 
 
    var isEmpty = false; 
 
    for(let i=0 ; i < elems.length;i++){ 
 
    if($(elems[i]).val().trim() != ''){ 
 
     isEmpty = true; 
 
     break; 
 
    } 
 
    } 
 
    $("#isempty").html(isEmpty?'All Not Empty':'All Empty'); 
 
    //return isEmpty; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<table> 
 
    <tr> 
 
    <td>abc</td> 
 
    <td>781</td> 
 
    <td><input id="barcodedb0" class="hide" type="text" /><input id="barcode0" class="hide" type="text" /></td> 
 
    </tr> 
 
    <tr> 
 
    <td>abc</td> 
 
    <td>781</td> 
 
    <td><input id="barcodedb1" class="hide" type="text" /><input id="barcode1" class="hide" type="text" /></td> 
 
    </tr> 
 
</table> 
 
<input onclick="checkEmpty()" type="button" value="check empty"/> 
 
<div id="isempty"></div>

に重複したIDの

関連する問題