Cakephp 3.6.14:如果选择输入具有值,则启用按钮

时间:2019-01-29 09:52:25

标签: javascript php jquery cakephp cakephp-3.x

我的代码中有一个选择控件,还有2个提交按钮。如果在选择框中没有完成选择,如何隐藏或禁用按钮之一?

代码如下:

//selection box
echo $this->Form->control('assigned_to',['options' => $usersTo, 'empty' => true, 'value' => $selectedUser]);

//submit buttons

<?= $this->Form->submit(__('Direct Assign'), ['name' => 'btn', 'class' => 'button']) ?> 
//this must be disabled or hidden if selection box is empty


<?= $this->Form->submit(__('Save'), ['name' => 'btn', 'class' => 'button']) ?>

2 个答案:

答案 0 :(得分:0)

  1. 首先,您不应该使用Form-> submit,该表单有一个Submit 按钮,我认为您应该改用postlink。同时更改 命名并为按钮创建不同的ID。
  2. 第二,您可以使用jquery进行此操作,您将听取您的选择 控制变更事件并禁用和启用或执行您想做的事情 到其他控件
<?php
$this->start("scriptBottom");
?>
<script>
$(document).ready(function () {
     $("assigned-to").change(function ()
     {
          var selected = $("#assigned-to option:selected").text();
          if(selected == 'OPTION 1')
          {
                 $("#btn").prop('disabled');
          }
          //do other things
}
});
</script>

<?php
$this->end();
echo $this->fetch('scriptBottom');
?>

答案 1 :(得分:0)

您可以使用javascript执行相同操作:

//selection box
echo $this->Form->control('assigned_to',['options' => $usersTo, 'empty' => true, 'value' => $selectedUser, 'id' => 'assigned_to']);

//submit buttons

<?= $this->Form->submit(__('Direct Assign'), ['name' => 'btn', 'class' => 'button', 'id' => 'direct_assign']) ?> 
//this must be disabled or hidden if selection box is empty


<?= $this->Form->submit(__('Save'), ['name' => 'btn', 'class' => 'button']) ?>


    $(document).ready(function(){
// check the value of assign selection box when document is ready
     let assignedValue = $("#assigned_to").val(); // get the value of assign selection box
      if(assignedValue == '') {
       $("#direct_assign").attr('disabled', true) // to disable direct assign button 
      }
    })

希望这会有所帮助!