如何从外部文件运行功能作为窗体的动作? [英] How to run function from external file as action for form?

查看:82
本文介绍了如何从外部文件运行功能作为窗体的动作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



目前我有一个类似下面的表单的HTML文件:

 < form id =my_formaction =javascript:OnAction()method ='post'> 
< / form>

我在script.js等外部文件中使用了OnAction()函数。


$ b $ 问题:

如果OnAction()函数位于外部文件(在html中,此函数运行)。
运行外部文件中的其他脚本(这意味着脚本文件连接正确)。



问题:



如何在我的表单的动作上运行OnAction()函数并将其保存在外部文件中?

绑定类似于:

  $('#my_form')。on('action',function(){
// OnAction here
});


解决方案

将您的HTML更改为:

 < form id =my_formaction =onSubmit =OnAction()method ='post'>< / form> 

您只需要这样做:

 函数OnAction(){
// OnAction here
};

或者更好的方法是将事件与Jquery结合使用:



HTML:

 < form id =my_formaction =method ='post'> < /形式> 

JS:

 < ()code> $(#my_form)。on(submit,function(){
/ * OnAction here * /
});


Description:

At present I have html file with a form like the following:

<form id="my_form" action="javascript:OnAction()" method='post' >
</form>

I have javascript function "OnAction()" in external file like script.js.

Problem:

OnAction() function isn't run, if it is located in external file (In html this function is runned). Other scripts in external file are runned (It means that script file is connected correctly).

Question:

How I can run OnAction() function on action of my form and hold it in external file?

Is exists way to bind something like:

$('#my_form').on('action', function() {
    // OnAction here
});

解决方案

Change your HTML to:

<form id="my_form" action="" onSubmit="OnAction()" method='post'></form>

You just need to do this:

function OnAction() {
    // OnAction here
};

Or better way is to put your event with Jquery:

HTML:

<form id="my_form" action="" method='post'></form>

JS:

$("#my_form").on("submit", function() {
    /* OnAction here*/
});

这篇关于如何从外部文件运行功能作为窗体的动作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆