提交表单时,检索表格中选中的所有复选框 [英] Retrieve all checkbox checked in the table when submitting form
本文介绍了提交表单时,检索表格中选中的所有复选框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的表单中有一个< table>
,其中包含从数据库中检索到的一些行,每行都放了一个复选框,
I have a <table>
in my form that contains some rows retrieved from database and on each row I put a checkbox, and
checkbox标签的名称是行记录的id
the name of the checkbox tag is the id of the row record
,值根据复选框1的状态而变化:选中, 0:未选中
and the value changes according to the state of checkbox 1 : checked, 0:unchecked
我想在用户提交表单时发送选定的行ID:
我不知道如何实现:
I want to send the selected rows id when user submit the form : I don't know how can I achieve that :
<table border=1 id="mondiv" class="hidden" >
<th></th>
<th>Numero</th>
<th>Nom</th>
<th>Prenom</th>
<th>Spécialité</th>
{% for item in users %}
<tr>
<td><input type="checkbox" name="{{ item.id }}" onchange="changeetat(this)" /></td>
<td>{{ item.id }}</td>
<td>{{ item.name }}</td>
<td>{{ item.lastname }}</td>
<td>{{ item.specialty }}</td>
</tr>
{% else %}
<h2>Aucun organisateur trouvé</h2>
{% endfor %}
</table>
这里是Javascript方法:
and here is the Javascript method:
function changeetat(element){
if(element.checked){
element.value = '1';
}
else{
element.value = '0';
}
}
我使用带有Symfony的twig模板
I use twig template with Symfony
推荐答案
如果你不使用jQuery而不是:
if you're not using jQuery than :
var inputs = document.getElementsByTagName("input"); //or
document.forms[0].elements;
var cbs = []; //will contain all checkboxes
var checked = []; //will contain all checked checkboxes
for (var i = 0; i < inputs.length; i++) {
if (inputs[i].type == "checkbox") {
cbs.push(inputs[i]);
if (inputs[i].checked) {
checked.push(inputs[i]);
}
}
}
var nbCbs = cbs.length; //number of checkboxes
var nbChecked = checked.length; //number of checked checkboxes
使用jQuery:
var cbs = $("input:checkbox:checked");
这篇关于提交表单时,检索表格中选中的所有复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文