简体   繁体   English

如何在Python中存储从Javascript动态生成的HTML表单元素?

[英]How to store dynamically generated HTML form elements from Javascript in Python?

I have an HTML form that a user can add an arbitrary amount of input fields to through jQuery. 我有一个HTML表单,用户可以通过jQuery向其中添加任意数量的输入字段。 The user is also able to remove any input field from any position. 用户还可以从任何位置删除任何输入字段。 My current implementation is that each new input box has an id of "field[i]" so when the form is posted it is processed in Python as field1, field2 field3, ...field[n] 我当前的实现是,每个新输入框的ID为“ field [i]”,因此在发布表单时,它将在Python中作为field1,field2,field3,... field [n]处理

i = 0
while self.request.get("field" + str(i)):
        temp = self.request.get("field" + str(i))
        someList.append(temp)
        i += 1

(Assume the JavaScript handles removing of deleted elements and sorts the field names prior to post for simplicity) (为简单起见,假设JavaScript处理删除的元素并在发布之前对字段名称进行排序)

This approach is working for me, but is there a better way to handle this situation? 这种方法对我有用,但是有更好的方法来处理这种情况吗? I feel like this is a very brute force method. 我觉得这是一种非常蛮力的方法。

Platform information: Python 2.5.4; 平台信息:Python 2.5.4; JavaScript; JavaScript的; DHTML; DHTML; jquery; jQuery的; Google App Engine Google App引擎

Edit: It appears that self.request.get_all() was the solution: GAE Doc 编辑:看来self.request.get_all()是解决方案: GAE Doc

You could serialize the data with javascript and pass it in as json. 您可以使用javascript序列化数据并将其作为json传递。 Then you would just have a dictionary to work with in python. 然后,您将只有一个可在python中使用的字典。 You would need something like simplejson , of course 当然,您将需要诸如simplejson之类的东西

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

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