2015-12-16 61 views
1

我已經禁用按鈕點擊的具體領域,如我使用jquery我可以取消這些禁用特定表單字段單擊

各個領域已經兩種形式在我的網頁這樣

< form method='post' action='' class='myform'> 
<input type='text' name='name_0'> 
<input type='email' name='email_0'> 
<input class='btn' type='submit'> 
</form> 

< form method='post' action='' class='myform'> 
<input type='text' name='name_1'> 
<input type='email' name='email_1'> 
<input class='btn' type='submit'> 
</form> 

$(document).ready(function(){        
    $('.btn').click(function(){ 
     $("input").prop('disabled', true); 
    }); 
}); 

但我不知道如何定位像NAME_0,EMAIL_0特定字段當我第一次提交按鈕,請幫我這個

+0

您是否想要在單擊按鈕時禁用特定輸入? – Kisaragi

+0

提交表單中的所有輸入 – Sikander

+0

是否要獲取特定字段或表單中的所有輸入或具有特定數字的所有字段或特定類型的所有字段?講清楚。 – Sheepy

回答

6

要禁用它們,你將使用屬性選擇:

$("input[name='name_0']").prop('disabled', true); 

jQuery Attribute Selectors

如果你有多個項目,你可以將它們組合起來:

$("input[name='name_0'],input[name='email_0']").prop('disabled', true); 

你更大的問題是,你想通過點擊的東西用一個類來確定形式。爲了縮小下來,你可以做一些DOM遍歷:

$(document).on('click', '.btn', function(event) { 
    // which form? this one! 
    var currentForm = $(this).closest('form'); 
    // disable all inputs for this form 
    currentForm.find('input').prop('disabled', true); 
}); 

現在我不必知道它輸入到禁止(如果我想禁用所有的)。我只需要在按鈕的表單中找到輸入。

+2

周杰倫你錯過了一個'_' – cmorrissey

+0

好抓!謝謝@cmorrissey! –

+0

看起來不錯,讓我試試這個 – Sikander

3

你應該閱讀有關selectors

$("input[name=name_0]").prop('disabled', true); 

或一個jQuery的解決方案可以通過使用.closest(目標吧).find()

$(document).ready(function(){        
    $('.btn').click(function(e){ 
     e.preventDefault(); 
     $(this).closest('form').find("input[type=text]").prop('disabled', true); 
     $(this).closest('form').find("input[type=email]").prop('disabled', true); 
    }); 
}); 

提交表單並禁用所有輸入並專注於下一個表格上的第一個輸入

$(document).ready(function(){        
    $('.myform').on('submit',function(e){ 
     e.preventDefault(); 
     $(this).closest('form').find("input").prop('disabled', true); 
     $(this).next('form').find('input:first').focus(); 
    }); 

}); 

Working Demo

+0

我可以通過多種vaues我的意思是類型如果我想diable按鈕和電子郵件以及什麼? – Sikander

+0

@Sikander你可以用這個$(this).closest('form')。find('input')。prop('disabled',true)來使用所有輸入。 ..我剛剛與我的答案這樣,因爲你已經說過,按鈕上點擊禁用特定的表單字段 –

+1

非常感謝先生最後一件事我如何將焦點移動到下一個表單如果表單進入循環 – Sikander