表的多个复选框的值存储在数组中并将该数组发送到flask

时间:2019-09-14 11:35:04

标签: javascript python html flask

我将根据表中的行数(在tbody内部)引入复选框。现在我想将复选框索引发送到flask应用程序路由,以便将复选框索引值存储在我的数据库中。我能够提醒复选框索引值(0,1,4 ....),但如何将此数组发送到flask

我已经尝试过了

$('#match').on("click", function() {
  var trs = $("input:checked").closest("tr"); //get tr elements of checked inputs
  var indexes = $.map(trs, function(tr) { return $(tr).index(); }); //make an array containing the indexes of these tr elements

  alert(indexes);
});

为表格创建一个复选框

$("#my_fo_id>tbody>tr").prepend("<input type='checkbox'  id='check_fo' class='checkBoxClass_fo' style='margin-top: 5px;' name='fo_box'/>");

按钮

<button id="match" class="button button1" type="submit">Match</button>

表格代码

<table class="responsive display fo_data sortable"  width="100%" id = "my_fo_id">
                    <tbody>
                      {% for row in row_data %}
                      <tr >
                          {% for col, row_ in zip(column_names, row) %}

                          <td >
                            {{ row_ }}                                
                          </td>
                          {% else %}
                          <td >
                            {{row_}}                              
                          </td>

                          {% endfor %}
                      </tr>
                      {% endfor %}
                    </tbody>

复选框值数组并提醒该数组

$('#match').on("click", function() {
  var trs = $("input:checked").closest("tr"); //get tr elements of checked inputs
  var indexes = $.map(trs, function(tr) { return $(tr).index(); }); //make an array containing the indexes of these tr elements

  alert(indexes);
});

我想将数组发送到flask应用程序路由(/ Match),以便将数组存储在数据库中

@app.route('/Match', methods=['GET','POST'])
def manual():

if request.method == 'POST':
    conn = sqlite3.connect('/C:/Users/patipra/Desktop/rdot_pac/database/test4.db')
    cr = conn.cursor()
    cr.execute("DROP TABLE IF EXISTS match_id")
    cr.execute("CREATE TABLE match_id (ID INTEGER)")
    return render_template('frame_set.html')   
else:        

    return render_template('home.html')

1 个答案:

答案 0 :(得分:0)

您似乎想在每次单击复选框时更新一组选定的行。您可以使用JSON和AJAX将数组发送到Flask。

let indexes_json = JSON.stringify(indexes);
$.post('/Match', indexes_json);

您将要处理失败案例。然后在另一侧,解压JSON数组。另一种选择是构造一个看起来像'/ Match?4&5&8'的URL,您可以使用$.get()进行查询,然后解压缩。