【问题标题】:multiple checkbox value of table store in an array and send the array to flask表中的多个复选框值存储在数组中并将数组发送到烧瓶
【发布时间】:2019-09-14 11:35:04
【问题描述】:

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

我试过了

$('#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);
});

我想将数组发送到烧瓶应用程序路由(/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')

【问题讨论】:

    标签: javascript python html flask


    【解决方案1】:

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

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

    你会想要处理失败的情况。然后在另一边,解压 JSON 数组。另一种选择是构建一个类似于“/Match?4&5&8”的 URL,您可以使用例如$.get() 进行查询,然后解压缩。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-05-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多