如何解决选中复选框时如何将元素添加到数组
我希望当我选中一个复选框时,将一个元素添加到数组中,而当我取消选中该复选框时。该代码有效,但不能正常工作,我不知道它来自哪里。
@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”。
最好的方法是从阵列中查找并删除该复选框。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。