2017-08-14 32 views
-1

我使用下面的代碼來計算用戶在表單中輸入的字段總數,但是如果它們未被選中,它會給我單選按鈕的數量,我想算單選按鈕,只有當他們在由用戶輸入的字段的總數檢查計算用戶填寫的字段總數

var filledInputs = $(':input').filter(function() { 
    return !!this.value; 
}).length; 

請建議

+0

請包括 –

回答

1

您當前的代碼不工作,因爲所有單選按鈕有value無論他們被檢查與否。爲了使filter()工作你需要詢問一個:checkbox元素的checked屬性:

var filledInputs = $(':checkbox').filter(function() { 
    return this.checked; 
}).length; 

話雖這麼說,你可以使代碼更通過:checked選擇簡單 - 有那麼沒有必要爲filter()在所有:

var filledInputs = $(':checkbox:checked').length; 
+0

我使用下面的HTML VAR totalFilledFields = $(目標).closest(」模式的內容 ')所有的HTML代碼中找到(':輸入:不使用(類型='hidden')')。not('button')。filter(function(){retu $(this).val()!=「」; })。長度; –

+0

好的,這有什麼問題嗎?其次,如果您需要添加更多信息,請編輯您的問題。註釋中的代碼幾乎不可讀 –

+0

var totalFilledFields = $(target).closest('。modal-content')。find(':input:not([type = hidden])')。not('button')。filter (function(){return $(this).val()!=「」;})。length; –