简体   繁体   English

动态创建输入然后从烧瓶输入获取数据

[英]Dynamically created inputs then getting data from inputs with flask

I wrote some HTML and Javascript to create a table that has input fields that appear. 我写了一些HTML和Javascript来创建一个包含输入字段的表。 Here is a picture of the HTML Table . 这是HTML表格的图片。 When the user keeps clicking the button to add more it looks like this: HTML Table More Input 当用户一直单击按钮添加更多时,它看起来像这样: HTML Table More Input

Here is the HTML code for the table and the javascript to create more inputs: 这是表的HTML代码和用于创建更多输入的javascript:

<form action="/information.html" method="POST">
    <table class="table table-borderless">
        <thead>
            <tr>
                <th style="text-align:center">Ticker</th>
                <th style="text-align:center">Shares</th>
            </tr>
        </thead>
        <tbody id='room_fileds'>
            <tr>
                <td style="text-align:center">
                    <fieldset class="form-group">
                        <input type="text" placeholder="AAPL"   name="stock1"/>
                     </fieldset>
                 </td>
                 <td style="text-align:center">
                     <fieldset class="form-group">
                        <input type="text" placeholder="100" name="weight1"/>
                     </fieldset>
                 </td>
             </tr>
        </tbody>
    </table>
</form>
<br/>
<input type="button" id="more_fields" onclick="add_fields();" value="Add More" /> 
<input type='submit' value='Submit'/>

<script>
var count = 0
function add_fields() {
count++
var county = count.toString();
var objTo = document.getElementById('room_fileds')
var divtest = document.createElement("tr");
divtest.innerHTML = '<td style="text-align:center"><input type="text"></td><td  style="text-align:center"><input type="text"></td>';
objTo.appendChild(divtest)
}
</script>

I am trying to use flask to get all the post input. 我正在尝试使用flask来获取所有的帖子输入。 Usually I have an input with a name such as stock1 and then I do the following with flask: 通常我有一个名称如stock1的输入,然后我用烧瓶执行以下操作:

stock1=request.form.get('stock1',type=str)

However, I am unsure of how to handle this type of dynamically created inputs. 但是,我不确定如何处理这种动态创建的输入。 I am not sure if the user will enter data into 1 or 2 or even 25 input boxes. 我不确定用户是否会将数据输入1或2或甚至25个输入框。 Is there a proper way to use flask to get all of this data if it is unknown how much data the user will enter? 如果不知道用户将输入多少数据,是否有正确的方法使用flask来获取所有这些数据? Possibly, I would like to get all of the tickers into a list and all of the shares into another list. 可能,我想把所有的代码放到一个列表中,并将所有的股票放到另一个列表中。

也许尝试这个存储库: https//github.com/sebkouba/dynamic-flask-form工作正常。

I noticed that if you want to have all dynamically created inputs with the same name that they must be a different name from the first input field that is not dynamically created. 我注意到,如果您希望所有动态创建的输入具有相同的名称,则它们必须是与非动态创建的第一个输入字段不同的名称。 So, I named the first input s0 and the rest of the dynamically created inputs s1. 因此,我将第一个输入s0和其余动态创建的输入s1命名为。 I can then call request.form.get() on s0 and can call request.form.getlist() on s1. 然后我可以在s0上调用request.form.get()并在s1上调用request.form.getlist()。 I can then append s0 to s1 and I have the list that I wanted, which is full of all the input data. 然后我可以将s0追加到s1,我有我想要的列表,其中包含所有输入数据。 However, it does not work if you let the dynamically created inputs have the same name as the first non dynamically created input. 但是,如果您让动态创建的输入与第一个非动态创建的输入具有相同的名称,则它不起作用。

I don´t know much about flask, but with this code, you can get the array of objects and insert them one by one with your server logic. 我不太了解flask,但是使用此代码,您可以获取对象数组并使用您的服务器逻辑逐个插入它们。

https://jsfiddle.net/8gdun4j0/ https://jsfiddle.net/8gdun4j0/

PS: used jquery PS:使用jquery

  arrayObject=[];
$("#subButon").on("click",function(){
for(var index=0;index<count+1;index++){
arrayObject.push(
{stock:$("#stock"+index).val(),
weight:$("#weight"+index).val()}
)
}
console.log(arrayObject);
alert("objetos guardados")
})

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

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