我的代码中有一个选择控件,还有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']) ?>
答案 0 :(得分:0)
<?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
}
})
希望这会有所帮助!