繁体   English   中英

HTML5 / CakePHP表单上的Click事件不起作用

[英]Click event on HTML5 / CakePHP form not working

这基本上是蛋糕的形式:

<?= $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() ?>

当前是jquery文件:

$(function() {
  $('.btn-save-cnl').bind('click', function(event) {
    /* Act on the event */
   $(this).prop('disabled', true);
  });
});

textarea“ user_text”已通过html5并具有必需的attr进行了验证。
我想防止在提交表单时双击。
我已经尝试过novalidate表单和bind / on / click事件,还向表单/按钮添加了id / name,但一无所获。

使用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']) ?>
  1. 您忘了添加jQuery CDN
  2. 也忘记使用以下命令fetch脚本:

    $this->start("scriptBottom"); $this->end(); echo $this->fetch('scriptBottom');

这是完整的工作代码:

<?= $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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM