简体   繁体   English

Javascript:有没有办法命名提交函数?

[英]Javascript: Is there a way to name submit functions?

I am trying to return the "value" variable to use on another function but it says that the "getData" is not defined我正在尝试返回“值”变量以在另一个 function 上使用,但它说“getData”未定义

 $('form').submit(function getForm(e){ e.preventDefault(); var value = $(this).data('test'); return value; }); fromForm = getForm(); function alertForm(){ alert(fromForm); }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <form data-test="1"> <input type="text"/> <input type="submit" /> </form> <form data-test="2"> <input type="text"/> <input type="submit" /> </form>

I think this is what you want我想这就是你想要的

$('form').submit(function(e){
    e.preventDefault();
    var value = $(this).data('test');
    alertForm(value);
});


function alertForm(fromForm){
    alert(fromForm);
}

What you are trying to do is to get data-test attirbute value and us it in your alertForm Function.您要做的是获取数据测试属性值并将其用于您的alertForm函数。 You can use a variable to having a greater scop to get this value from the handler of the the submit function but it is not clean.您可以使用具有更大范围的变量从提交 function 的处理程序中获取此值,但它不干净。 The best way is to get the value directly from the dom as follow.最好的方法是直接从 dom 中获取值,如下所示。

    function alertForm(){
    var fromForm =$("form").data('test');
        alert(fromForm);
    }

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

相关问题 有没有办法在 JavaScript 中命名箭头函数? - Is there a way to name arrow functions in JavaScript? 有没有办法在Chrome的Inspector中实时查看Javascript函数(函数名称)的执行情况? - Is there a way to see what Javascript functions (the name of the functions) execute in real time in Chrome's Inspector? 使用Javascript提交具有多种功能的输入类型 - Input type submit with multiple functions in Javascript 两个功能的Javascript按钮。 提交和点击 - Javascript button for two functions. Submit & OnClick 2功能必须正确才能提交表格。 (JavaScript) - 2 Functions have to be true to submit the form. (Javascript) Javascript-在表单提交时执行所有验证功能 - Javascript - Executing all the validation functions on form submit 有没有办法让 2 个函数具有相同的名称? - Is there a way to have 2 functions with the same name? Javascript - 使用变量名创建函数 - Javascript - Create functions with variable name 为什么javascript允许$作为函数名称? - why javascript permits $ for name of functions? javascript中有没有办法按名称调用多个函数并以数组格式获取结果? - Is there any way in javascript to call multiple functions by name and get their result in array format?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM