简体   繁体   中英

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:

$(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.
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.

Using cakePHP 3.5.3, Jquery 3.2.1 and Bootstrap 4

默认情况下,按钮不提交表单,请尝试将type => submit添加到按钮:

<?= $this->Form->button('Save', ['class' => 'btn btn-info w-75 btn-save-cnl', 'type' => 'submit']) ?>
  1. You've forget to add te jQuery CDN
  2. Also forget to fetch your script using the :

    $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');
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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