2012-03-27 15 views
1

我試圖應用.each方法,以便此頁面上的每個「響應文本區域」都是單獨定位的(我在一個頁面上有多個表單提交,而我希望每個提交按鈕都被禁用,直到在相應的文本區域中輸入文本爲止)。但我不知道該把它放在哪裏。我可以鏈接方法嗎?如何將jquery函數應用於每個實例

(這是所有的的document.ready)

$('.submit').attr('disabled', 'disabled'); 
$('.response-text-area').each.keyup(function() { 
    if ($('.response-text-area').val() == "") { 
     $('.submit').attr('disabled', 'disabled'); 
    } 
    else { 
     $('.submit').removeAttr('disabled'); 
    } 
}); 

回答

3

只需刪除該.each(衛生組織語法不正確,反正)。該事件將被綁定到每個類別爲response-text-area的控件。

$('.response-text-area').keyup(function(){ 
    if($(this).val() == ""){ 
     // I'd need to see your markup to provide code here. 
     // You need to find the correct submit button in relationship 
     // to the current item. Something similar to this: 
     $(this).parent().find('.submit').attr('disabled', 'disabled'); 
    } 
    else{ 
     $(this).parent().find('.submit').removeAttr('disabled'); 
    } 
}) 
0

你並不需要使用每一個,因爲這應該工作以及

$('.response-text-area').bind("keyup", function(){ 
    if($('.response-text-area').val() == ""){ 
     $('.submit').attr('disabled','disabled'); 
    } 
    else{ 
     $('.submit').removeAttr('disabled'); 
    } 
}) 
0
$('.response-text-area').keyup(function() { 
    $(".submit", this.form).prop("disabled", !this.value); 
}); 
相關問題