我使用下面的代碼來計算用戶在表單中輸入的字段總數,但是如果它們未被選中,它會給我單選按鈕的數量,我想算單選按鈕,只有當他們在由用戶輸入的字段的總數檢查計算用戶填寫的字段總數
var filledInputs = $(':input').filter(function() {
return !!this.value;
}).length;
請建議
我使用下面的代碼來計算用戶在表單中輸入的字段總數,但是如果它們未被選中,它會給我單選按鈕的數量,我想算單選按鈕,只有當他們在由用戶輸入的字段的總數檢查計算用戶填寫的字段總數
var filledInputs = $(':input').filter(function() {
return !!this.value;
}).length;
請建議
您當前的代碼不工作,因爲所有單選按鈕有value
無論他們被檢查與否。爲了使filter()
工作你需要詢問一個:checkbox
元素的checked
屬性:
var filledInputs = $(':checkbox').filter(function() {
return this.checked;
}).length;
話雖這麼說,你可以使代碼更通過:checked
選擇簡單 - 有那麼沒有必要爲filter()
在所有:
var filledInputs = $(':checkbox:checked').length;
我使用下面的HTML VAR totalFilledFields = $(目標).closest(」模式的內容 ')所有的HTML代碼中找到(':輸入:不使用(類型='hidden')')。not('button')。filter(function(){retu $(this).val()!=「」; })。長度; –
好的,這有什麼問題嗎?其次,如果您需要添加更多信息,請編輯您的問題。註釋中的代碼幾乎不可讀 –
var totalFilledFields = $(target).closest('。modal-content')。find(':input:not([type = hidden])')。not('button')。filter (function(){return $(this).val()!=「」;})。length; –
請包括 –