[英]Click event on HTML5 / CakePHP form not working
This is basically the form in cake: 这基本上是蛋糕的形式:
<?= $this->Form->create($container, ['class' => 'form-horizontal', 'autocomplete' => 'off']) ?>
// some inputs
<?= $this->Form->control('user_text', ['label' => false, 'class' => 'form-control']) ?>
<?= $this->Form->button('Save', ['class' => 'btn btn-info w-75 btn-save-cnl']) ?> <i class="fa fa-circle-o-notch fa-spin fa-2x fa-fw align-middle invisible"></i>
<?= $this->Form->end() ?>
and currently the jquery file: 当前是jquery文件:
$(function() {
$('.btn-save-cnl').bind('click', function(event) {
/* Act on the event */
$(this).prop('disabled', true);
});
});
the textarea "user_text" is validated via html5 with the required attr. textarea“ user_text”已通过html5并具有必需的attr进行了验证。
I want to prevent double click when submitting the form. 我想防止在提交表单时双击。
I already tried novalidate form and bind/on/click event, also adding id/name to form/button but got nothing. 我已经尝试过novalidate表单和bind / on / click事件,还向表单/按钮添加了id / name,但一无所获。
Using cakePHP 3.5.3, Jquery 3.2.1 and Bootstrap 4 使用cakePHP 3.5.3,Jquery 3.2.1和Bootstrap 4
默认情况下,按钮不提交表单,请尝试将type => submit添加到按钮:
<?= $this->Form->button('Save', ['class' => 'btn btn-info w-75 btn-save-cnl', 'type' => 'submit']) ?>
Also forget to fetch
your script using the : 也忘记使用以下命令
fetch
脚本:
$this->start("scriptBottom"); $this->end(); echo $this->fetch('scriptBottom');
This is a full working code: 这是完整的工作代码:
<?= $this->Form->create($container, ['class' => 'form-horizontal', 'autocomplete' => 'off']) ?>
// some inputs
<?= $this->Form->control('user_text', ['label' => false, 'class' => 'form-control']) ?>
<?= $this->Form->button('Save', ['class' => 'btn btn-info w-75 btn-save-cnl']) ?> <i class="fa fa-circle-o-notch fa-spin fa-2x fa-fw align-middle invisible"></i>
<?= $this->Form->end() ?>
<?php
$this->Html->script([
'https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js'
],
['block' => 'script']);
$this->start("scriptBottom");
?>
<script>
$(function() {
$('.btn-save-cnl').bind('click', function(event) {
/* Act on the event */
alert("CLICKED");
$(this).prop('disabled', true);
});
});
</script>
<?php
$this->end();
echo $this->fetch('scriptBottom');
?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.