【问题标题】:Cakephp 3.6.14: Make datetime field readonlyCakephp 3.6.14:将日期时间字段设为只读
【发布时间】:2019-02-19 08:07:56
【问题描述】:

我在CakePHP 中有一个日期时间控件,我正在尝试将其设为只读(readonly 属性适用于文本字段)。

这就是我尝试过的:

CakePHP 方式

echo $this->Form->control('date_start', ['empty' => true, 'readonly' => 'readonly']);

echo $this->Form->control('date_start', ['empty' => true, 'readonly' => 'true']);

JavaScript

$this->Html->scriptBlock("document.getElementById('#datestart').readonly = true;");

echo $this->Form->control('date_start', ['id' => 'datestart', 'empty' => true]);

但它们都不起作用。我怎样才能实现我想要的。

【问题讨论】:

    标签: javascript php cakephp datetimepicker readonly


    【解决方案1】:

    您正在寻找 disabled 属性:https://www.w3schools.com/TAGS/att_input_disabled.asp 请注意,如果输入被禁用,则不会提交(见下文)。

    CakePHP:

    echo $this->Form->control('date_start', ['empty' => true, 'disabled' => 'true']);
    

    Jquery,获取或设置值(抱歉,我从未尝试过使用原生 Javascript):

    $( elem ).prop( "disabled" )
    
    $( elem ).prop( "disabled", true )
    

    如果输入被禁用,则不会提交。 也许这并不完全是你所期望的。 有关(许多)可能的解决方法,请参阅此问题: HTML form readonly SELECT tag/input

    【讨论】:

    • 但是如果它被禁用,值将不会被发布到控制器......对吗?我想保存价值。例如我加载已经存储在数据库中的日期时间值,用户可以看到这个值但不能更改它。用户可以编辑其他字段,然后保存更改
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多