如何通过JavaScript设置表单动作? [英] How to set form action through JavaScript?

查看:124
本文介绍了如何通过JavaScript设置表单动作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个HTML表单,其操作应该通过JavaScript动态设置。我该怎么做?这是我想要实现的:

I have an HTML form whose action should be set dynamically through JavaScript. How do I do it? Here is what I am trying to achieve:

<script type="text/javascript">
function get_action() { // inside script tags
 return form_action;
 }
</script>

<form action=get_action()>
...
</form>


推荐答案

onXXX 。只需在window onload中指定它即可。

You cannot invoke JavaScript functions in standard HTML attributes other than onXXX. Just assign it during window onload.

<script type="text/javascript">
    window.onload = function() {
        document.myform.action = get_action();
    }

    function get_action() {
        return form_action;
    }
</script>

<form name="myform">
    ...
</form>

你看到我给出了一个 name ,因此可以在 document 中轻松访问。

You see that I've given the form a name, so that it's easily accessible in document.

另外,您也可以在<$

Alternatively, you can also do it during submit event:

<script type="text/javascript">
    function get_action(form) {
        form.action = form_action;
    }
</script>

<form onsubmit="get_action(this);">
    ...
</form>

这篇关于如何通过JavaScript设置表单动作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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