2014-10-17 105 views
0

在我的表格中,我有不同類型的輸入。我現在想要做的是選擇具有特定名稱的所有文本字段,然後檢查是否全部填充。例如,我有電子郵件,用戶名,機構和地址字段。如何選擇type =「text」但具有特定名稱的所有輸入?

我的目標是隱藏一條消息textArea,並只會顯示它,如果所有提到的字段都有值。我如何綁定所有這些字段,以便當所有這些字段被填充時,如果清除了這些字段中的任何一個,則textArea消息將顯示並再次隱藏?

我在想$("input[name=email], input[name=address], input[name=username], input[name=address]"),我不認爲這是有效的。

消息textArea的方式將基於來自這些字段的輸入來填充。 例子:

To Whom It May Concern: 

.......................Bunch of text preassigned................ 
................................................................ 

Regards, 
username.val()<email.val()> 
institution.val() 
address.val() 

目前,在我的表格,如果用戶跳過了機構領域,我的textarea應該是這樣的:

To Whom It May Concern: 

.......................Bunch of text preassigned................ 
.......................Bunch of text preassigned................ 

Regards, 
username.val()<email.val()> 

address.val() 

這樣,如果我隱藏textarea的,直到所有上述領域都填充後,我會將我的消息textArea預填充好。

回答

0

套用css類的所有目標文本輸入

$(document).ready(function() { 
 
    $(':text.required').on('input', function() { 
 
    var NOTfilled = $(':text.required').filter(function() { 
 
     return !this.value.trim(); 
 
    }); 
 
    if (NOTfilled.length) { 
 
     $('.hidefirst').hide(); 
 
    } else { 
 
     $('.hidefirst').show(); 
 
    } 
 
    }); 
 
});
.hidefirst { 
 
    display: none; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<form> 
 
    <input type="text" name="field1" class="required" /> 
 
    <input type="text" name="field2" class="required" /> 
 
    <input type="text" name="field3" class="required" /> 
 
    <input type="text" name="field4" class="required" /> 
 
    <input type="text" name="field5" class="required" /> 
 
    <input type="text" name="field6" class="required" /> 
 
    <textarea class="hidefirst" name="info">Hidden Info</textarea> 
 
</form>

+0

我怎麼能測試空間?我嘗試了'if($ .trim(NOTfilled.val())===「」){$('。hidefirst')。hide();}'檢查textarea是否會隱藏,如果用戶只提供一個空格處。 – euler 2014-10-18 05:48:21

0

可以做這樣的事情:

HTML:

<form id="theForm"> 
    <input type="email" name="example"/> 
    <input type="text" name="example"/> 
</form> 

<textarea id="txtArea">...</textarea> 

CSS:

#txtArea { 
    display:none; 
} 

的jQuery:

// Whenever an input with the name example is modified 
$('input[name=example]').keyup(function(){ 
    // Check if any input in the form with the name example is empty 
    var emptyInput = $("#theForm").find('input[name=example]').filter(function(){ 
     return this.value === ""; 
    }); 
    // If one or more inputs named example are empty, hide the textarea, else show it. 
    emptyInput.length > 0 ? $("#txtArea").hide() : $("#txtArea").show(); 
}); 

jsFiddle here

相關問題