Cakephp:当复选框被选中/取消选中时启用/禁用文本框

问题描述:

需要帮助来解决这个问题!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以及我们完成的代码的更多修正) –

使用三元运算来切换复选框的变化禁用状态

$('[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>