简体   繁体   English

提交动态生成的表单

[英]Submitting a dynamically generated form

I'm creating a website, and I have a form that gets created by Django. 我正在创建一个网站,并且有一个由Django创建的表单。 Once the page is loaded, the user can add text inputs to the form by clicking a button. 页面加载后,用户可以通过单击按钮将文本输入添加到表单。 The problem is that when the submit button is clicked, only the text inputs that were originally created by Django get submitted. 问题在于,当单击“提交”按钮时,仅最初由Django创建的文本输入会被提交。 In other words, the extra text inputs that were added dynamically don't get submitted. 换句话说,动态添加的额外文本输入不会提交。

I'm guessing that this is due to the fact that the submit button is only "aware" of form elements that were present when the page was loaded, and not dynamically loaded elements. 我猜这是由于以下事实:提交按钮仅“知道”页面加载时存在的表单元素,而不是动态加载的元素。 With that in mind, I'm guessing I need to use some kind of Javascript in order to submit all of the form elements, including the dynamically added ones, but I can't figure out how to do it. 考虑到这一点,我想我需要使用某种Javascript才能提交所有表单元素,包括动态添加的表单元素,但是我不知道该怎么做。

I've tried the jQuery submit function, but I don't really know what I'm supposed to do with it. 我已经尝试过jQuery submit功能,但是我真的不知道该怎么做。 Any tips would be appreciated! 任何提示将不胜感激!

EDIT: Here's a code snippet, which shows what the HTML looks like after 2 more text inputs have been added dynamically to the "origin" 编辑:这是一个代码段,显示了另外2个文本输入已动态添加到“来源”后的HTML外观

<table>
    <form class="dataInput" action="/foner/116" method="post">
    <input type='hidden' name='csrfmiddlewaretoken' value='YYuqTzXUVosu1s2HD3zS00DpoPwQ7N0k' />
    <tbody class="origin">
        <tr>
            <th>
                <label>Origin:</label>
            </th>
            <td>
                <input id="id_Origin-0" maxlength="200" name="Origin-0" type="text" value="A native of Georgia" /> // PRESENT AT PAGE LOAD
            </td>
            <td>
                <button class="adder origin">+</button> // USER CLICKS THIS TO APPEND MORE TEXT INPUTS TO THE FORM
            </td>
        </tr>
        <tr>
            <th>
            </th>
            <td>
                <input type="text" value="" maxlength="200" name="origin[]"></input> // DYNAMICALLY ADDED
            </td>
        </tr>
        <tr>
            <th>
            </th>
            <td>
                <input type="text" value="" maxlength="200" name="origin[]"></input> // DYNAMICALLY ADDED
            </td>
        </tr>
    </tbody>
    <tr>
        <th>
            <label>Notes:</label>
        </th>
        <td>
            <input class="submitButton" type="submit" value="S" />
        </td>
    </tr>
    </form>
</table>

Ok, I was able to solve the problem. 好的,我能够解决问题。 I had a table that was arranging all of the text inputs for the form, and the table also enclosed the form itself. 我有一个表,用于安排表单的所有文本输入,并且该表还包含表单本身。 It turns out that by inverting this, and enclosing the table inside of the form, all of the dynamically generated inputs get POSTed successfully. 事实证明,通过将其反转并将表格包含在表单内,所有动态生成的输入都将成功发布。 Thanks again to those who gave input in the comments above -- it's always helpful to get other opinions & perspectives. 再次感谢在上面的评论中提供意见的人员-征求其他意见和观点总是很有帮助的。

So my question is (I know you're not supposed to ask questions in answers, but in case anyone feels like responding...): How was I supposed to know this? 所以我的问题是(我知道您不应该在答案中提出问题,但万一有人想回应...):我应该怎么知道的? If you're using a compiled language, this is something that a compiler would probably catch for you. 如果您使用的是编译语言,则编译器可能会为您抓住这一点。 Is it just the kind of thing that you get the hang of with experience? 您只是经验的掌握者吗? Are there any books that would help me to get a handle on elementary problems like this? 是否有任何书籍可以帮助我解决此类基本问题? I find web development to be very tedious and frustrating because I often get hung up for long periods of time on trivial errors like this, and I'm assuming it doesn't have to be this way; 我发现Web开发非常繁琐且令人沮丧,因为我经常长时间困扰于这种琐碎的错误,并且我认为它不一定是这种方式。 I just don't quite know how to improve. 我只是不太了解如何改进。

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

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