取决于另一个复选框

问题描述 投票:0回答:1

我有两个要在jQuery中成为依赖项的复选框(ON / OFF),这意味着当我选中第一个(checkbox1)为ON时,第二个(checkbox2)将自动为ON,我已经尝试过此解决方案但它不起作用,有人可以帮我吗。

//php
 $cus_centre = $this->createElement('checkbox', 'checkbox1');
        $cus_centre->setLabel($translate->_("Checkbox1"));
        // ->setAttrib("disabled", "disabled");
        $this->addElement($cus_centre);

    $checkbox2 = $this->createElement('checkbox', 'checkbox2');
    $checkbox2->setLabel($translate->_("Checkbox2"));
    // ->setAttrib("disabled", "disabled");
    $this->addElement($checkbox2);   

//jQuery
      $('#checkbox1').iphoneStyle({
            onChange:function(){
            if($("#checkbox1").is(':checked')){
                $("#checkbox1").val(1);
                $("#checkbox2").attr('checked', true);
                $("#checkbox2").val(1);

          }
       }
   });

My Checkboxes view

javascript jquery zend-framework bootstrap-4
1个回答
0
投票

您可以像这样简单地使其。使用功能(更改监听器)内部的$(this)检查是否已选中第一个复选框。如果是这样,它将标记第二个。

$('#checkbox1').change(function() {
  if(this.checked) {
    $("#checkbox2").prop("checked", true);
  } 
});
© www.soinside.com 2019 - 2024. All rights reserved.