基于另一个下拉列表的动态下拉列表

时间:2014-10-02 02:21:12

标签: javascript jquery html ajax flask

我有两个下拉框,如何从第一个选择一个值( get_tests()),第二个是填充依赖于第一个的值( get_options(试验))?

#!/usr/bin/env python

import flask

app = flask.Flask(__name__)

def get_tests():
return ["Test1", "Test2"]

def get_options(test):
options = {"Test1": ["Test1_option_a", "Test1_option_b"],
      "Test2": ["Test2_option_a", "Test2_option_b"]}
return options[test]

@app.route('/search', methods=["GET"])
def search():
test = flask.request.args.get('tests')

return flask.render_template('search.html', tests=get_tests())

@app.route('/results', methods=["GET"])
def results():
test = flask.request.args.get('tests')
option = flask.request.args.get('options')
return flask.render_template('results.html', test=test, option=option)

search.html:

<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
</head>
<body>
  <form enctype="multipart/form-data" action="{{url_for('results')}}" method="get">

      <select name="tests">
      {% for test in tests %}
        <option selected value="{{test}}">{{test}}</option>
      {% endfor %}
      </select>
      <BR/>
      <select name="options">
      <option value=" " selected="selected">Please select one of the options above first</option>
      {% for option in options %}
        <option selected value="{{option}}">{{option}}</option>
      {% endfor %}
      </select>

      <BR/>
      <input type="submit" name="go" value="SEARCH"/>
  </form>
</body>
</html>

results.html:

<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
</head>
<body>
{{test}}
{{option}}
</body>
</html>

0 个答案:

没有答案