[英]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.