简体   繁体   中英

Rails Javascript onsubmit validate() Uncaught ReferenceError: validate is not defined

I have a form of the following characteristics:

= nested_form_for reservation, html: { onsubmit: "return validate()"} do ...

And the following file reservation.js:

jQuery(function() {
  $('div.modal').on('loaded.bs.modal', function(e) {
    ...some code...
  });
  $('div.modal').on('shown.bs.modal', function(e) {
    ...some code...
  });

  function validate(){
    var guests = 0;
    $('.guests-input').each(function(i, e){
      guests = guests + $(e).val();
    });
    if (guests < 1){
      alert('Sum of percentage must be 100');
      return false;
    }
    return true;
  }
});

But, when I click the submit button I get the error:

Uncaught ReferenceError: validate is not defined

I don't know where should I define it. I've searched other questions but I couldn't solve my problem with them. I'm not entirely sure either where should I look.

I'll edit with more information if needed.

正如Rory McCrossan在评论中所说,解决方案是将validate()定义移出jQuery的document.ready处理程序之外。

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