选中复选框时如何将元素添加到数组

问题描述

我希望当我选中一个复选框时,将一个元素添加到数组中,而当我取消选中该复选框时。该代码有效,但不能正常工作,我不知道它来自哪里。

@app.route('/list',methods=['GET','POST'])
def list():
    if request.method == 'POST':
        participant = Participant.query.all()
    return render_template('admin/list.html',participant=participant)

解决方法

数字到您的数组,但是您想通过 shift 删除它,这是行不通的,因为推将元素放在末尾并移位从beinnig拿走它。
因此,只需使用 pop 从头开始使用它即可。

var tableau= ["0"];

var checkBox_numero = document.getElementById("numero");

    checkBox_numero.addEventListener('change',function () {
        if (this.checked) {
            // Checkbox is checked..
            tableau.push("numero");
        } else {
            // Checkbox is not checked..
            tableau.pop();
        }
         console.log(tableau);
    });
    console.log(tableau);
<input type='checkbox' id='numero'>

,

基本上,[[0]]是数组中的第一个元素。然后将numero推送到数组,因此看起来像这样:[“ 0”,numero]。

然后,当您调用shift时:它将删除第一个元素“ 0”。

最好的方法是从阵列中查找并删除该复选框。