2017-05-09 135 views
0

需要幫助來解決這個問題!Cakephp:當複選框被選中/取消選中時啓用/禁用文本框

使用CakePHP,我會列出顯示名稱(某些文本),複選框和文本框。我需要的是當複選框正在檢查,t extbox應啓用,否則應該禁用。我試過了。但它並不按我的願望工作。

這裏是代碼和它的jQuery。

<tbody> 
      <?php $count = 0; ?> 
      <?php foreach ($get_resources['learnings'] as $learnings): ?> 
       <tr><td> 
        <?php $control_id = $this->SDNTextUtils->cleanCssIdentifier($learnings['name']); ?> 

      <div class="switch"> 
     <?= $this->Form->label($control_id, __($learnings['name'])); ?></div></td> 
    <td> 
    <div class="switch"> 
    <?= $this->Form->checkbox("lrng_permission[$count][permission]", ['class' => 'cmn-toggle cmn-toggle-round', 'id' => $control_id, 'name'=>'idd']); ?> 


    <?= $this->Form->label($learnings['name'], ''); ?> 



    <?= $this->Form->hidden("lrng_permission[$count][resource_name]", ['value' => $learnings['name']]); ?> 

    <?= $this->Form->hidden("lrng_permission[$count][resource_type]", ['value' => 'Learning']); ?></td> 

    <td> <?= $this->Form->number('',['label'=>false,'id'=>'check1','disabled' => 'disabled']); ?> 

    </td></tr> 
    </div> 

<?php $count ++; ?> 
<?php endforeach; ?> 
    </tbody> 

//這裏是JQuery的

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
<script type="text/javascript"> 


    $('#idd').'change',function(){ 
     var checked=$('#idd').is(':checked'); 
     if(checked==true) 
     { 
      $('#check1').attr('disabled',false); 
     } else { 

      $('#check1').attr('disabled',true); 
     } 
     }); 

</script> 
+0

請及時更新所提供的HTML蛋糕代碼。所以我們可以測試。此外jQuery代碼是完全混亂。它需要'$('#idd')。on('change',function(){'(我認爲它不會起作用,直到你顯示的HTML以及我們完成的代碼的更多修正) –

回答

0

使用三元運算來切換複選框的變化禁用狀態

$('[name=idd]').on('change', function() { 
 
    console.log(this.checked); 
 
    $('#check1').prop('disabled', this.checked); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<input type="checkbox" name="idd"> 
 
<textarea id="check1">textarea</textarea>

相關問題