2017-05-24 60 views
0
結尾

我在所有行中都有一個輸入表,我需要檢查是否所有輸入都填充了值 我試圖使用選擇器$(「[id $ ='txtBarcode' ]「)選擇以txtBarcode結尾的所有輸入,但似乎不起作用。 有什麼問題?所有文本的jquery選擇符以

$(document).ready(function() { 
 
    $("#cmdConferma").click(function() { 
 
    AllFilled(); 
 
    }); 
 
}); 
 

 
function AllFilled() { 
 
    $("[id$='txtBarcode']").each(function() { 
 
    $this = $(this); 
 
    // var value = $("[id$='txtBarcode']").value(); 
 
    if ($this.value() == '') { 
 
     return confirm('are you sure to exit without fill?'); 
 
    } 
 
    }); 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<table class="table_style" cellspacing="0" rules="all" border="1" id="dgrRighe" style="width:100%;border-collapse:collapse;"> 
 
    <tr> 
 
    <td> 
 
     <input name="dgrRighe$ctl02$txtBarcode" type="text" value="1" id="dgrRighe_ctl02_txtBarcode" /> 
 
    </td> 
 
    <td> 
 
     <input name="dgrRighe$ctl03$txtBarcode" type="text" id="dgrRighe_ctl03_txtBarcode" /> 
 
    </td> 
 
    <td> 
 
     <input name="dgrRighe$ctl04$txtBarcode" type="text" id="dgrRighe_ctl04_txtBarcode" /> 
 
    </td> 
 
    </tr> 
 
</table> 
 
<input type="submit" name="cmdConferma" value="exit" id="cmdConferma" class="button" />

回答

4

那是因爲.value()是無效的jQuery方法。您需要使用.val()代替:

if ($this.val() == '') 

還,如果你的目標是讓元素的值,就沒有必要創建元素的jQuery對象。您可以使用純JavaScript。我也會建議使用trim()來防止用戶輸入空字符:

if (this.value.trim() == '') 
+0

'.value()'在javascript中也不是方法。它的一個屬性。爲了使用'.value',不需要將dom對象轉換成jquery對象。使用'this.value'將返回值 –