2014-06-26 75 views
0

我無法刪除文本和使用jquery代碼禁用文本框,下面是我的代碼。無論是選中還是取消選中複選框,警報也正常運行。請建議。禁用文本框單擊複選框不工作使用jquery

<script> 
     $(document).ready(function(){ 
      var arr= new Array('1','2','3','4','5'); 
      for(var i=0;i<arr.length;i++) 
       { 
       $("#Q4x1_"+arr[i]).click(function(){ 
        if($("#Q4x1_"+arr[i]).prop('checked',true)) 
         { 
          alert("hi"); 
          $("#Q4x2_"+arr[i]).val(''); 
          $("#Q4x2_"+arr[i]).prop('readOnly',true); 
         } 
        else 
         { 
          $("#Q4x2_"+arr[i]).val('testing'); 
               $("#Q4x2_"+arr[i]).prop('readOnly',false); 
         } 
       }); 
       } 
     }); 
     </script> 
    </head> 

    <body> 
    <table border="1"> 
    <tr> 
    <td><input type="checkbox" id="Q4x1_1"></td> 
    <td><input type="text" id="Q4x2_1"></td> 
    </tr> 

    <tr> 
    <td><input type="checkbox" id="Q4x1_2"></td> 
    <td><input type="text" id="Q4x2_2"></td> 
    </tr> 

    <tr> 
    <td><input type="checkbox" id="Q4x1_3"></td> 
    <td><input type="text" id="Q4x2_3"></td> 
    </tr> 

    <tr> 
    <td><input type="checkbox" id="Q4x1_4"></td> 
    <td><input type="text" id="Q4x2_4"></td> 
    </tr> 

    <tr> 
    <td><input type="checkbox" id="Q4x1_5"></td> 
    <td><input type="text" id="Q4x2_5"></td> 
    </tr> 


    </table> 
    </body> 
    </html> 
+0

您應該使用'的this.checked'代替'$( 「#Q4x1 _」 + ARR [i])。prop('checked',true)' – Satpal

+0

但是使用這種方式是錯誤的嗎?我只做了一個複選框和一個文本框。它的工作方式很好。 – Vaibhav

+0

繼承人演示http://jsfiddle.net/2kuqa/更清潔。 – Satpal

回答

0

您可以簡單地使用:

$('input:checkbox').change(function(){ 
var checked = $(this).prop('checked'); 
var nextinp=$(this).parent().next().find('input'); 
nextinp.prop('disabled', checked); 
if(checked) 
    nextinp.val(''); 
}); 

Working Demo

相關問題