【问题标题】:Use javascript to set action in django form使用 javascript 以 django 形式设置操作
【发布时间】:2017-09-20 10:54:14
【问题描述】:

django 1.10中,我需要将参数从一个html模板传回view.py,最后再次渲染相同的视图。

问题在于在 html 中设置 action 以使用 javascript。

这是我的 html 模板:

<form method='POST' action='/res/{{link}}/{{page_id}}/{{email}}/'> 
      <input type="hidden" name="email" value="{{email}}">
      <input type="image" type='submit' src={% static "assets/images/twitter_icon.png" %}>
</form> 

action 中的{{page_id}} 在用户与网页交互时发生变化(使用下拉选择器)。以下 javascript 将能够捕捉到这一点:

<script>
function post_redirect_link() {
    var page_id = document.getElementById("sel1").options.selectedIndex;
    return '/res/{{link}}/'+page_id+'/{{email}}/'  
}
</script>

我尝试将action动态设置为:

<form method='POST' action='post_redirect_link()'> 
      <input type="hidden" name="email" value="{{email}}">
      <input type="image" type='submit' src={% static "assets/images/twitter_icon.png" %}>
</form> 

但是,我得到了/res/correct_link/post_redirect_link()/correct_email 类型的重定向,其中“post_redirect_link()”被视为字符串,而不是 js 方法返回的值。

有什么建议吗?

【问题讨论】:

  • 尝试执行&lt;form action="javascript:;" onsubmit="myFunction(this)"&gt; 并在您的函数中,对您想要的任何地址进行post 调用。

标签: javascript jquery html django post


【解决方案1】:

尝试执行以下操作。

<form action="#" method='POST'>
  <script>
     function changeAction() {
         var page_id = document.getElementById("sel1").options.selectedIndex;
         var url = '/res/{{link}}/'+page_id+'/{{email}}/';
         document.forms[0].action = url;
     }
  </script>
  <input type="hidden" name="email" value="{{email}}">
  <input type="image" type='submit' onmouseover="changeAction()" src={% static "assets/images/twitter_icon.png" %}>

</form>

【讨论】:

    猜你喜欢
    • 2021-07-27
    • 1970-01-01
    • 1970-01-01
    • 2017-12-14
    • 2011-12-25
    • 2020-09-19
    • 1970-01-01
    • 2013-10-11
    • 1970-01-01
    相关资源
    最近更新 更多