2011-05-05 70 views
0

在以下內容中,我使用過濾器('input:text,textarea')將輸入字段設置爲只讀,並且使它們不透明。但是,此選擇器僅選擇文本輸入,而不是文本區域。我究竟做錯了什麼?爲什麼這jQuery選擇器不選擇textarea?

<!DOCTYPE html> 
<html> 
    <head> 
    <title>setReadOnly() Test</title> 
    <link rel="stylesheet" type="text/css" href="../styles/core.css"> 
    <link rel="stylesheet" type="text/css" href="test.setReadOnly.css"> 
    <script type="text/javascript" src="../scripts/jquery-1.4.js"></script> 
    <script type="text/javascript" src="../scripts/jqia2.support.js"></script> 
    <script type="text/javascript"> 
     (function($){ 
      $.fn.setReadOnly = function(readonly) { 
      return this.filter('input:text,textarea') 
       .attr('readOnly',readonly) 
       .css('opacity', readonly ? 0.5 : 1.0) 
       .end(); 
      }; 
     })(jQuery); 
    </script> 
    <script type="text/javascript"> 
     $(function(){ 
     $('#sameAddressControl').click(function(){ 
      var same = this.checked; 
      $('#billAddress').val(same ? $('#shipAddress').val():''); 
      $('#billCity').val(same ? $('#shipCity').val():''); 
      $('#billState').val(same ? $('#shipState').val():''); 
      $('#billZip').val(same ? $('#shipZip').val():''); 
      $('#bill_random_field').val(same ? $('#ship_random_field').val():''); 
      $('#billingAddress input').setReadOnly(same); 
     }); 
     }); 
    </script> 
    </head> 

    <body> 

    <div data-module="Test setReadOnly()"> 

     <form name="testForm"> 
     <div> 
      <label>First name:</label> 
      <input type="text" name="firstName" id="firstName"/> 
     </div> 
     <div> 
      <label>Last name:</label> 
      <input type="text" name="lastName" id="lastName"/> 
     </div> 
     <div id="shippingAddress"> 
      <h2>Shipping address</h2> 
      <div> 
      <label>Street address:</label> 
      <input type="text" name="shipAddress" id="shipAddress"/> 
      </div> 
      <div> 
      <label>City, state, zip, random_field:</label> 
      <input type="text" name="shipCity" id="shipCity"/> 
      <input type="text" name="shipState" id="shipState"/> 
      <input type="text" name="shipZip" id="shipZip"/> 
      <textarea name="random_field" rows="2" cols="20" id="ship_random_field"></textarea> 
      </div> 
     </div> 
     <div id="billingAddress"> 
      <h2>Billing address</h2> 
      <div> 
      <input type="checkbox" id="sameAddressControl"/> 
      Billing address is same as shipping address 
      </div> 
      <div> 
      <label>Street address:</label> 
      <input type="text" name="billAddress" 
        id="billAddress"/> 
      </div> 
      <div> 
      <label>City, state, zip, random_field:</label> 
      <input type="text" name="billCity" id="billCity"/> 
      <input type="text" name="billState" id="billState"/> 
      <input type="text" name="billZip" id="billZip"/> 
      <textarea name="random_field" rows="2" cols="20" id="bill_random_field"></textarea> 
      </div> 
     </div> 
     </form> 
    </div> 

    </body> 
</html> 

回答

2

嘗試將線$('#billingAddress input').setReadOnly(same);改變$('#billingAddress input,textarea').setReadOnly(same);

更新

實際上,必須像你的評論稱:。

+0

有趣的是,這並沒有工作,但將其更改爲'$('#billingAddress input,#billingAddress textarea')。setReadOnly(same);'did do work。我很疑惑爲什麼你的建議沒有奏效。 – jela 2011-05-05 13:42:29

+0

建議不起作用,因爲您試圖過濾元素本身,而不是內容。相反,您可以嘗試'$('#billingAddress')。find('*').setReadOnly(same)',或者重寫您的過濾器以執行'this.find('*')..'。 – Shagglez 2011-05-05 13:46:56

+0

對不起,我忘了把'input,textarea' – Tulio 2011-05-05 13:47:49