2013-01-03 22 views
3

我目前選擇下列方式使用jQuery形式的所有輸入字段:jQuery的如何選擇所有輸入字段除了提交按鈕

$('#new_user_form *').filter(':input').each(function(key, value) { 

這工作得很好,雖然它選擇提交輸入以及我不希望它這樣做。 有沒有簡單的方法讓它忽略提交按鈕?

謝謝!

+1

的可能重複的[ jQuery選擇器的問題(如何選擇一個窗體上的所有輸入字段EXCEPT按鈕和複選框)](http://stackoverflow.com/questions/3129151/jquery-selector-question-how-to-select-all-input-fields-除了按鈕之外) – Niko

+0

由於文檔中的性能警告,我假設你使用了'.filter'。儘管選擇* all * descendants然後過濾是錯誤的方法。如果你想得到所有的表單元素,只需使用多重選擇器:'$('#new_user_form *')。find('input:not([type =「submit」]),button,textarea,select')'。 –

回答

11

您可以使用:not()

$('#new_user_form input:not([type="submit"])') 

同時,應注意與:input和其他非標準選擇。它們與實現document.querySelectorAll的瀏覽器上的本機選擇器沒有相同的速度優勢。

+1

或者它可能更容易閱讀:'$('#new_user_form input')。not('[type =「submit」]') –

+0

更容易閱讀,更高效執行。 –

+0

@ Beetroot-Beetroot:'.not()'放慢速度:http://jsperf.com/not-vs-not23 – Blender

2

嘗試這種情況:

var inputs = $('#new_user_form input').not(':input[type=submit]'); 

$(inputs).each(function() { 
    // your code... 
});​ 

FIDDLE

UPDATE(基於@Blender輸入)

var inputs = $('#new_user_form input').not('[type=submit]'); 

$(inputs).each(function() { 
    // your code... 
});​ 

FIDDLE2

+1

'inputs'已經是一個jQuery對象,因此不需要再次將它重新包裝在jQuery函數中。 – Blender

+0

是的,我只是測試它。它的工作沒有再次輸入。感謝您指出:) –

相關問題