Python 捕获HTML下拉值
我正在学习使用瓶子来创建web应用程序。我是HTML新手 如何捕获下拉列表值?在一个简单的例子中,我要求用户输入他想记录多少人的年龄。情况如下:Python 捕获HTML下拉值,python,python-2.7,web,bottle,Python,Python 2.7,Web,Bottle,我正在学习使用瓶子来创建web应用程序。我是HTML新手 如何捕获下拉列表值?在一个简单的例子中,我要求用户输入他想记录多少人的年龄。情况如下: import bottle as bt @bt.route('/persons', method = 'GET') def count_persons(): return ''' <h3>We are going to collect data on the ages of a number of people<
import bottle as bt
@bt.route('/persons', method = 'GET')
def count_persons():
return '''
<h3>We are going to collect data on the ages of a number of people</h3>
<p>Use the drop down box below to select the number of people who's age you would like to record.</p>
<p>
<select id="persons">
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
</select, name = "persons">
<br/>
<input type="submit" name="continue" value="continue">
'''
@bt.route('/persons', method = 'POST')
def render_template():
persons = bt.request.GET.get('persons', '')
return 'You have chosen ' + str(persons)
bt.run(host = 'localhost', port = 8080, debug = True)
将瓶子作为bt导入
@bt.route('/persons',method='GET')
def count_persons():
返回“”'
我们将收集一些人的年龄数据
使用下面的下拉框选择要记录的同龄人数
2.
3.
4.
5.
'''
@bt.route('/persons',method='POST')
def render_模板():
persons=bt.request.GET.GET('persons','')
返回“您已选择”+str(人)
bt.run(主机='localhost',端口=8080,调试=True)
我还尝试了
bt.request.forms('persons')
。在我看来,所选值应该可以通过下拉列表的id
访问。这不对吗?您必须使用html表单,以便将数据发送到服务器
import bottle as bt
@bt.route('/persons', method = 'GET')
def count_persons():
return '''
<form action="/persons" method="post">
<h3>We are going to collect data on the ages of a number of people</h3>
<p>Use the drop down box below to select the number of people who's age you would like to record.</p>
<p>
<select id="persons">
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
</select, name = "persons">
<br/>
<input type="submit" name="continue" value="continue">
</form>
'''
@bt.route('/persons', method = 'POST')
def render_template():
persons = bt.request.POST.get('persons', '')
return 'You have chosen ' + str(persons)
bt.run(host = 'localhost', port = 8080, debug = True)
将瓶子作为bt导入
@bt.route('/persons',method='GET')
def count_persons():
返回“”'
我们将收集一些人的年龄数据
使用下面的下拉框选择要记录的同龄人数
2.
3.
4.
5.
'''
@bt.route('/persons',method='POST')
def render_模板():
persons=bt.request.POST.get('persons','')
返回“您已选择”+str(人)
bt.run(主机='localhost',端口=8080,调试=True)
单击submit按钮将数据发送到服务器,还将导致重新加载页面。如果不想重新加载页面,则必须使用XmlHttpRequest。谢谢您的回答。老实说,我还不完全清楚。根据我捕获的输入,我将向用户返回一个保存在.tpl文件中的表单,该文件使用下拉列表中的整数值创建正确的表单。所以我想我不希望它重新加载。在sumbission之后给出的示例中,我只想返回一个简单字符串作为实验。我这样做完全错了吗?我想我根本不了解HTTP方法,这是主要问题。