简体   繁体   English

如何使用onsubmit调用js函数

[英]How to call the js function with onsubmit

I am trying to do set a validation for the description field. 我正在尝试为描述字段设置验证。 But js is not calling when i click the add button. 但是,当我单击添加按钮时,js没有调用。 Please help me how to call the js function. 请帮助我如何调用js函数。

view file 查看文件

        = nested_form_for Task.new, :html=>{:multipart => true, :id => "vali" }, turboboost: true do |f|

         //form_fields

        = f.submit 'Add', class: 'btn-theme pull-right', :onsubmit => "validate()";   

javascript javascript

$(document).ready(function() {
    $("#vali").on("submit",validate({
        rules: {
        "task[description]": {required: true},
        },
    });
});

You can't directly call a function with parameters like that as you are executing it immediately and passing the result of the function to the submit event registration. 在立即执行并将该函数的结果传递给Submit Event注册时,您无法直接使用具有此类参数的函数进行调用。

Add an anonymous function around your code: 在您的代码周围添加一个匿名函数:

$(document).ready(function() {
    $("#vali").on("submit",function (){
        validate({
            rules: {
            "task[description]": {required: true},
            });
    });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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