从输入框中提取列表列表,在flask中提取复选框值

时间:2017-11-03 12:42:17

标签: python html flask

这是我的index.html

<!DOCTYPE html>
<script>
  function add_field()
  {
    var total_text=document.getElementsByClassName("input_text");
    total_text=total_text.length+1;
    field_div = document.getElementById("field_div");
    new_input = "<li id='input_text"+total_text+
    "_wrapper'><input type='text' class='input_text' name='input_text[]' id='input_text"+
    total_text+"' placeholder='Enter Text'>"+
    "<label><input name='input_text"+total_text+"' id='input_text[]' type='radio' value='1'>1</label>"+
    "<label><input name='input_text"+total_text+"' type='radio' id='input_text[]' value='2'>2</label>"+
    "</li>";
    field_div.insertAdjacentHTML('beforeend',new_input);
  }
  function remove_field()
  {
    var total_text=document.getElementsByClassName("input_text");
    document.getElementById("input_text"+total_text.length+"_wrapper").remove();
  }
</script>
{% extends "bootstrap/base.html" %}
{% block content %}

<div class = "container">
  <h1>Give the words</h1>
  <form action='/results' method="post">
    <div id="wrapper">
      <input type="button" value="Add TextBox" onclick="add_field();">
      <input type="button" value="Remove TextBox" onclick="remove_field();">
      <ol id="field_div">

      </ol>
    </div>
    <input type='submit' value='Select'>
  </form>
</div>
{% endblock %}

我的views.py如下:

from flask import render_template, request, url_for
from app import app
from .translit import *

@app.route('/')
def search():
    return render_template('index.html')


@app.route('/results', methods = ['GET', 'POST'])
def results():
    if request.method == 'GET':
        return redirect(url_for('/'))
    else:
        values = getperm(request.form.getlist('input_text[]'))
        print(request.form.getlist('input_text[]'))
        return render_template('results.html',
                               values = values)

现在,我可以从所有输入文本中提取输入作为列表吗? 如何从列表中获取每个<li>的值,从而创建列表列表?

举个例子, 如果我输入

a 1
b 2

我应该能够将结果提取为[[a,1],[b,2]]

2 个答案:

答案 0 :(得分:1)

我们应该操纵复选框的value属性。

为每个文本框添加了复选框。

app.py

from flask import Flask, render_template, url_for, request

app = Flask(__name__)

@app.route('/')
def search():
    return render_template('dynamic_input.html')

@app.route('/results', methods = ['GET', 'POST'])
def results():
    if request.method == 'GET':
        return redirect(url_for('search'))
    else:
        input_values = request.form.getlist('input_text[]')
        checkbox_values = request.form.getlist('input_checkbox')
        return render_template('dynamic_input_results.html',
                               input_values = input_values,
                               checkbox_values = checkbox_values)

if __name__ == '__main__':
    app.run(debug = True)

dynamic_input.html

<!DOCTYPE html>
<script>
  function add_field()
  {
    var total_text=document.getElementsByClassName("input_text");
    total_text=total_text.length+1;
    field_div = document.getElementById("field_div");
    new_input = "<li id='input_text"+total_text+"_wrapper'>";
    new_input += "<input type='text' class='input_text' name='input_text[]' id='input_text"+
    total_text+"' placeholder='Enter Text'>";
    new_input += "<input type='checkbox' name='input_checkbox' value='"+total_text+"' id='input_checkbox"+
    total_text+"'";
    new_input += "</li>";
    field_div.insertAdjacentHTML('beforeend',new_input);
  }
  function remove_field()
  {
    var total_text=document.getElementsByClassName("input_text");
    document.getElementById("input_text"+total_text.length+"_wrapper").remove();     
  }
</script>

<div class = "container">
  <h1>Give the words</h1>
  <form action='/results' method="post">
    <div id="wrapper">
      <input type="button" value="Add TextBox" onclick="add_field();">
      <input type="button" value="Remove TextBox" onclick="remove_field();">
      <ol id="field_div">

      </ol>
    </div>
    <input type='submit' value='Select'>
  </form>
</div>

dynamic_input_results.html

<ul>
{% for value in input_values %}
    <li>{{value}}</li>
{% endfor %}
<hr>
{% for value in checkbox_values %}
    <li>{{value}}</li>
{% endfor %}

</ul>

输出:

added checkbox

checkbox_output

答案 1 :(得分:0)

可能不是你所希望的,但如果你的结果列表足够小,这可能会成功。

#assuming your result list is ll

alplhabet = list(string.ascii_lowercase)

if len(ll)<=len(alplhabet):
    res = []
    for i in map(None,alplhabet[:len(ll)],ll):
        res.append(list(i))
    return res