我正在尝试添加复选框,以便可以从数据库中删除多条记录。我给多个输入相同的name,但request.form.getlist即使选中了一些框,也会返回一个空列表。
{% for order in orders %}
<input type="checkbox" name="select_button" value="{{ order.order_id }}">
{% endfor %}
Run Code Online (Sandbox Code Playgroud)
@mod_order.route('_delete_multiple_confirmation', methods=['GET', 'POST'])
def delete_multiple_orders_confirmation():
orders = (request.form.getlist('select_button'))
user_id = request.args.get('user_id')
response_context = {'orders': orders, 'user_id': user_id}
return render_template('delete_multiple_order_confirmation.html', **response_context)
Run Code Online (Sandbox Code Playgroud)
小智 5
这个让我失望了好几个小时!!希望这篇文章可以帮助人们节省时间。
当您提供多个输入时,您的输入类型会隐式更改为数组。这意味着您必须从数组中提取结果。您可以通过在输入变量 "[]" 的名称末尾添加这 2 个字符来实现:
在你的情况下,我们得到: orders = (request.form.getlist('select_button[]'))
全栈示例:
==> 在您的 html 中使用引导程序
<select class="selectpicker" multiple title="input list" id="inputlist">
<option>input1</option>
<option>input2</option>
<option>input3</option>
</select>
<small>Click Go! to proceed : </small>
<button class="myConfirmButton btn btn-primary">Go!</button>
<small><div id="myFeedback"></div></small>
Run Code Online (Sandbox Code Playgroud)
==> 在你的 jquery/javascript 中
<script>
$(document).ready(function(){
$(".myConfirmButton").click(function(){
// this line creates the array of all the checked inputs
var values = $('#listinput').val()
$.ajax({
url:"/processlist",
dataType:'html',
type:'post',
contentType: 'application/x-www-form-urlencoded',
data : {
listinput: values
},
success: function( data ){
$('#myFeedback').html( data );
},
error: function( errorThrown ){
$('#myFeedback').html( errorThrown );
}
});
});
});
</script>
Run Code Online (Sandbox Code Playgroud)
==> 在你的 python 烧瓶代码中
@app.route("/processlist",methods=['GET', 'POST'])
def processlist():
myinput=request.form.getlist('listinput[]') # here you add [] at the end
return '<br>'.join(myinput) # shows the total input
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1962 次 |
| 最近记录: |