简体   繁体   English

将全局javascript变量传递到操作表单的参数中

[英]Pass a global javascript variable into argument of an action form

I would like to include a global javascript variable that it is dynamically updated into the argument of an action form. 我想包含一个全局javascript变量,该变量会动态更新为操作表单的参数。 Previously, I was simply using django template syntax to include the django variable string returned by the view into the action form but now that it is updated I need to come up with a way to pass this updated javascript variable into the GET method. 以前,我只是使用django模板语法将视图返回的django变量字符串包含到操作表单中,但是现在它已更新,我需要想出一种方法来将此更新的javascript变量传递到GET方法中。 I save the variable as a window property "window.subgraph_query = 'foo'" this variable is supposed to change over time so it needs to be updated also in the form argument as well. 我将变量另存为窗口属性“ window.subgraph_query ='foo'”,该变量应该随时间变化,因此也需要在form参数中对其进行更新。 How can I reach this? 我怎样才能做到这一点? I think it should be easy 我认为应该很容易

Thank you! 谢谢!

<script type="text/javascript">

    // BEFORE
    var subgraph_query = "{{ subgraph_query }}";

    //NOW
    window.subgraph_query = "{{ subgraph_query }}";

</script>

// BEFORE
<form action="/graph/network_to_gml/{{ subgraph_query }}" method="get">
   <input name="export-node-list" id="export-node-list"type="text"/>
</form>

// NOW

<form action="/graph/network_to_gml/JAVASCRIPT_VARIABLE" method="get">
   <input name="export-node-list" id="export-node-list"type="text"/>
</form>

You need to add a hidden input field to your form and assign a value of a js variable. 您需要在表单中添加一个隐藏的输入字段并分配js变量的值。 this way it won't be visible for a user but your script will receive it. 这样,用户将看不到它,但是您的脚本会收到它。 Example of a hidden field type: 隐藏字段类型的示例:

 <input type="hidden" name="country" id='hidden1' value="">

and in JS: 在JS中:

document.getElementById('hidden1').value = 'Poland';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM