如何将javascript数组值传递给python烧瓶

时间:2018-02-11 09:27:39

标签: javascript python arrays web flask

我有一个html表单,它在提交后生成值数组,我想将这些值数组传递给python flask。

我尝试使用字段 id ="结果" 的隐藏表单。但是烧瓶没有认出它只能从名称中获取价值。

我不知道该怎么做,我是Flask框架的新手。

这是我的javascript代码:



function add_element_to_array()
{
     array[x] = ([
         document.getElementById("user").value,
         document.getElementById("customer").value,
         document.getElementById("product").value,
         document.getElementById("start").value,
         document.getElementById("end").value,
         document.getElementById("status").value,
         document.getElementById("description").value
     ]);
     alert("Element: " + array[x] + " Added at index " + x);
     x++;
         document.getElementById("user").value = "";
         document.getElementById("customer").value = "";
         document.getElementById("product").value = "";
         document.getElementById("start").value = "";
         document.getElementById("end").value = "";
         document.getElementById("status").value = "";
         document.getElementById("description").value = "";
}

function display_array()
{
   var e = "<hr/>";
   //var myJsonString;

   for (var y=0; y<array.length; y++)
   {
     e += "Element " + y + " = " + array[y] + "<br/>";
     //myJsonString = JSON.stringify(array[y]);
     //console.log(myJsonString)
       //return array[y];
   }

   document.getElementById("Result").innerHTML = e;
}

</script>
&#13;
&#13;
&#13;

&#13;
&#13;
<form action="/flaskprocess" method="POST">
  <input type="hidden" id="Result" value="" />
  <input type="submit" value="Submit" />
</form>
&#13;
&#13;
&#13;

我想将此 document.getElementById(&#34;结果&#34;)。innerHTML = e; 处理到烧瓶。我尝试了很多方法,但没有得到。

请帮助我。

谢谢。

1 个答案:

答案 0 :(得分:0)

我试过了。在将值设置为隐藏类型ID后,退出简单,将名称 属性提供给隐藏字段。这将有助于您

<input type="hidden" name="hidden" id="Result">
document.getElementById("Result").value = e;

这会将您的数组值设置为隐藏类型ID。

flask.py
values=request.form.getlist('hidden')