简体   繁体   English

禁用/启用文本框(取决于复选框)

[英]disable/enable text-box depending on check-boxes

I'm using cakePHP framework to work on my project. 我正在使用cakePHP框架来处理我的项目。

How do I enable a textbox when a the checkbox is check, and disable it when it's unchecked? 如何在选中复选框时启用文本框,而在未选中复选框时将其禁用? I tried javascript but it isn't working and I think it is because of my 'id'. 我尝试使用javascript,但无法正常运行,我认为这是因为我的“ id”。 How should I declare my 'id'? 我应该如何声明我的“身份证”?

Here is my code. 这是我的代码。 It is in a forLoop. 它在forLoop中。


echo $this->Form->checkbox('menu_item_id',
                            array(
                                    'value' => $items[$i]['MenuItem']['menu_item_id'],
                                    'hiddenField' => false,
                                    'name' => 'data[OrderItem][menu_item_id][]',
                                    'label' => false,
                                    'div' => false,
                                    )
                                ); 

echo $this->Form->input('quantity', 
                        array(
                            'style'=>'width:65px; height:25px;',
                            'name' => 'data[OrderItem][quantity][]',
                            'div' => false,
                            'label' => false
                            )
                        );

echo $this->Form->input('notes', 
                        array(
                            'style'=>'width:65px; height:25px;',
                            'name' => 'data[OrderItem][notes][]',
                            'div' => false,
                            'label' => false
                            )
                        );

By the way, I am a beginner both in cakePHP and Javascript. 顺便说一下,我是CakePHP和Javascript的初学者。 Thanks. 谢谢。

I made some changes. 我做了一些改变。 In my script, I added this 在我的脚本中,我添加了这个



    
        function changeStatus(){
            if(document.getElementById("my_checkbox").checked == true){
                document.getElementById("input_field").disabled= false;
                alert('a');
            } else {
                document.getElementById("input_field").disabled= true;
                alert("B");
            }
        }
    

*alerts are for testing. *警报仅供测试。 it goes to the proper block, but it does not enabled/disabled the textfield. 它会转到适当的块,但不会启用/禁用文本字段。 Do you have any idea why? 你知道为什么吗?

By dfault cakephp assigns id to any element with normal convention which is UsersMenuItemId (I am assuming users is your model here). 通过dfault cakephp将id分配给任何具有常规约定的元素,即UsersMenuItemId (我假设用户是您的模型)。 But you can assign id with other htmlOptions like 但是您可以将id与其他htmlOptions一起分配

echo $this->Form->checkbox('menu_item_id',
                        array(
                                'id' => 'my_checkbox',
                                'value' => $items[$i]['MenuItem']['menu_item_id'],
                                'hiddenField' => false,
                                'name' => 'data[OrderItem][menu_item_id][]',
                                'label' => false,
                                'div' => false,
                                )
                            );

Now write a basic script: 现在编写一个基本脚本:

<script>
  $(document).ready(function(){
      $('#my_checkbox').event('click', function(){
          if($('#my_checkbox').is(':checked')){
             $('#input_field').removeAttr('disabled');                 
          } else {
             $('#input_field').attr('disabled','disabled');
          }
      });
  });
</script>

In checkbox array => add 复选框数组=>添加

"onclick'=>'changeStatus(this);"

if this keyword not works for you then replace that with checkbox id 'menu_item_id' 如果关键字不适合您,则将其替换为复选框ID“ menu_item_id”

then write normail javascript function to disable inputbox 然后编写normail javascript函数以禁用输入框

 
 
 
 
  
  
  function changeStatus(obj){ if(obj.checked){ document.getElementById("quantity").disabled =true; }
 
 
  

Single line JS is to use below line, it will toggle between two states 单行JS用于下面的行,它将在两种状态之间切换

 
 
 
 
  
  
  document.getElementById("quantity").disabled =obj.checked;
 
 
  

Update : Check whether the below code working for you 更新 :检查以下代码是否对您有用

 echo $this->Form->checkbox('menu_item_id_'.$i, array( 'value' => $items[$i]['MenuItem']['menu_item_id'], 'hiddenField' => false, 'name' => 'data[OrderItem][menu_item_id][]', 'label' => false, 'div' => false, "onclick'=>'changeStatus(this,$i);" ) ); echo $this->Form->input('quantity_'.$i, array( 'style'=>'width:65px; height:25px;', 'name' => 'data[OrderItem][quantity][]', 'div' => false, 'label' => false ) ); function changeStatus(obj,index){ document.getElementById("quantity_"+index).disabled = ! obj.checked; } 
echo $this->Form->checkbox('menu_item_id',
                            array(
                                    'value' => $items[$i]['MenuItem']['menu_item_id'],
                                    'hiddenField' => false,
                                    'name' => 'data[OrderItem][menu_item_id][]',
                                    'label' => false,
                                    'div' => false,
                                    )
                                ); 

here first field you declared 'menu_item_id' is for table name or we can say model name. 在这里,您声明为“ menu_item_id”的第一个字段用于表名称,或者我们可以说模型名称。

there is different option for assigning id to input like: 有不同的方法可以为输入分配ID,例如:

echo $this->Form->checkbox('modelname',
                                array(
                                        'value' => $items[$i]['MenuItem']['menu_item_id'],
                                        'hiddenField' => false,
                                        'name' => 'data[OrderItem][menu_item_id][]',
                                        'label' => false,
                                        'div' => false,
                                        'id'=>'your-id'
                                        )
                                    ); 

but keep in mind that id must be unique on the page. 但请记住,id在页面上必须是唯一的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM