我在表中引入复选框,具体取决于表中的行数(在 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')
最佳答案
您似乎希望在每次单击复选框时更新选定行的集合。您可以使用 JSON 和 AJAX 将数组发送到 Flask。
let indexes_json = JSON.stringify(indexes);
$.post('/Match', indexes_json);
您将需要处理失败案例。然后在另一侧,解压 JSON 数组。另一种选择是构造一个类似于“/Match?4&5&8”的 URL,您可以使用例如 $.get()
查询该 URL,然后解压。
关于javascript - 表的多个复选框值存储在数组中并将数组发送到flask,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57935107/