2016-04-21 108 views
1

我選擇所有輸入jQuery中,像這樣的:jQuery的選擇所有輸入型除外隱藏

$('input').each(function() { 
... 
}); 

但有可能產生像說選擇與輸入型隱藏之外的所有輸入異常。

我知道我可以選擇輸入與特定類型的,我需要,但看起來不那麼好,我認爲

編輯:對不起,發錯了例子,我的意思是這樣的:

document.forms["product"].getElementsByTagName("input"); 

使用:input:hidden選擇.not方法排除隱藏

回答

3

試試這個:你可以利用:not[type="hidden"]

$(function(){ 
 
    $('input:not([type="hidden"])').each(function(){ 
 
    alert($(this).val()); 
 
    }); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<input value="1"> 
 
<input type="radio" value="2"> 
 
<input type="hidden" value="3"> 
 
<input type="checkbox" value="4">

+1

良好的漁獲物。重新閱讀說**類型**。+1 – BenG

+0

是的,謝謝@BenG! –

0

使用的例外元素。

.not將從集中刪除匹配元素

$('input').not(':input:hidden').each(function() { 
... 
}); 
0

有使用各種方法的元件。我寧願用:

$('input:visible').each(); 

注意::visible不與type="hidden"選擇input

或者你可以使用一個長一點的方法:

$('input:not([type="hidden"]').each(); 

或者,

$('input').not('[type="hidden"]').each(); 

API參考:visible-selectornot-selectornot

0

使用此:

$('input:not[type=hidden]').each(function(){ 

}); 
0

由於您的更新

我的意思是這樣的:

document.forms["product"].getElementsByTagName("input"); 

與喜異常dden

嘗試:

document.forms['product'].querySelectorAll('input:not([type="hidden"])') 
+0

所以我不能使用getElementsByTagName? –

+0

你可以,但那不會*例外*。那麼您需要通過結果來檢查類型。 – BenG

+0

如果您使用的是jQuery,那麼請使用@BhushanKawadkar答案。 – BenG

相關問題