我在CakePHP
中有一个日期时间控件,并且我试图将其设置为只读(readonly
属性在文本字段中可以正常使用)。
这就是我尝试过的:
CakePHP方式:
echo $this->Form->control('date_start', ['empty' => true, 'readonly' => 'readonly']);
OR
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]);
但是它们都不起作用。我如何实现自己想要的。
答案 0 :(得分:0)
您正在寻找已禁用属性: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