2012-03-02 45 views
0

我有一個button input,我要禁用它是這樣的:改變與jQuery的的document.ready標籤的屬性複製它

$(document).ready(function() { 
       if ($('#participants tr').length < 3) 
        jQuery("#send").attr('disabled', true); 
       else 
        jQuery("#send").attr('disabled', false); 
      }); 

它成功地禁止,但是當啓用條件爲真,這部分被複制在嵌套方式:

編輯:

<form> 
<fieldset> 
<table id="participants"> 
    .... 
</table> 
    </fieldset> 
</form> 

<input type="button" value="send" onClick="sendMail()" id="send" /> 
<fieldset> 
... 
<form> 
<table id="participants"> 
    <tr> 
     <td><a onClick='f(id)' >del</a></td> 
    </tr> 
</table> 
</form> 
</fieldset> 
<input type="button" value="send" onClick="sendMail()" id="send" /> 

table有p的某些行參與者每行都有一個del鏈接,點擊它會導致從數據庫和表中刪除參與者,當表中的行數小於3時我將禁用send button 爲什麼?

+3

你必須要更加明確。它重複了什麼? – Purag 2012-03-02 06:06:18

+0

我在這段代碼中沒有看到任何東西,使它重複另一個按鈕 – XepterX 2012-03-02 06:08:40

+0

@Purmou:對不起!我編輯了這個問題。 – 2012-03-02 06:13:54

回答

1

嘗試removeAttr

$(document).ready(function() { 
       if ($('#participants tr').length < 3) 
        jQuery("#send").attr('disabled', disabled); 
       else 
        jQuery("#send").removeAttr('disabled'); 
      }); 

UPDATE:

我不知道什麼是你想實現的,你沒有默認情況下禁用的按鈕DOM準備這樣使他們沒有任何意義我必須錯過一些東西,但是從你提供的代碼和我的理解來看,如果你修改標記爲

<form> 
<fieldset> 
<table id="participants1"> 
    <tr> 
     <td>1</td> 
    </tr> 
    <tr> 
     <td>2</td> 
    </tr> 
    <tr> 
     <td>3</td> 
    </tr> 
</table> 
    </fieldset> 
    <input type="button" value="send" onClick="sendMail()" class="send" /> 
</form> 
<fieldset> 
<form> 
<table id="participants"> 
    <tr> 
     <td>only one</td> 
    </tr> 
</table> 
    <input type="button" value="send" onClick="sendMail()" class="send" /> 
</form> 
</fieldset> 

請注意,ID現在是唯一的,我已將類send應用到每個發送按鈕,我也採取了窗體內的發送按鈕。 jQuery的部分是爲後續

$("form").each(function(){ 
var $this = $(this); 
var trCount = $this.find("table tr").length; 
    console.log(trCount); 
    if(trCount>2){ 
     console.log($this.find(".send")); 
    $this.find(".send").attr("disabled","disabled"); 
    }//else do nothing   
}); 

什麼在上面的代碼,通過頁面上的所有表單元素髮生的是它的迭代和尋找表格中他們則行計數檢查,如果超過指定的長度禁用它這種形式的按鈕..

DEMO

+0

tnx但它沒有工作! – 2012-03-02 06:12:44

+1

你有相同的ID – Rafay 2012-03-02 08:02:25

+0

但我沒有! :( – 2012-03-02 08:22:57

0

,而不是這個

 if ($('#participants tr').length < 3) 
      jQuery("#send").attr('disabled', true); 
     else 
      jQuery("#send").attr('disabled', false); 

嘗試

$('#participants').each(function(){ 

     if ($(this).find('tr').ength < 3) 
      $(this).parents('fieldset').next("#send").attr('disabled', true); 
     else 
      $(this).parents('fieldset').next("#send").attr('disabled', false); 

}); 
+0

tnx,它不起作用! – 2012-03-02 08:22:30