Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
132 views
in Technique[技术] by (71.8m points)

javascript - 如何通过JavaScript设置表单操作?(How to set form action through JavaScript?)

I have an HTML form whose action should be set dynamically through JavaScript.(我有一个HTML表单,其动作应该通过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>   ask by umar translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You cannot invoke JavaScript functions in standard HTML attributes other than onXXX .(您不能在onXXX以外的标准HTML属性中调用JavaScript函数。)

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> You see that I've given the form a name , so that it's easily accessible in document .(您看到我已经为表单指定了name ,因此可以在document轻松访问。) Alternatively, you can also do it during submit event:(或者,您也可以在submit活动期间执行此操作:) <script type="text/javascript"> function get_action(form) { form.action = form_action; } </script> <form onsubmit="get_action(this);"> ... </form>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...