我有下表行:
<table class="authors-list">
<tr>
<td style="font-size:10px;"><a class="deleteRow"> <img src="delete2.png" /></a></td>
<td ><input type="text" id="product1" name="product1" class="rounded"/></td>
<td ><input type="text" size='5' id="qty1" name="qty1" class="rounded"/></td>
<td class="tdcheckbox"><input type="checkbox" id="h09_1" name="h09_1" class="rounded"/></td>
<td class="tdcheckbox"><input type="checkbox" id="h12_1" name="h12_1" class="rounded"/></td>
<td class="tdcheckbox"><input type="checkbox" id="h15_1" name="h15_1" class="rounded"/></td>
<td class="tdcheckbox"><input type="checkbox" id="h18_1" name="h18_1" class="rounded"/></td>
<td class="tdcheckbox"><input type="checkbox" id="h21_1" name="h21_1" class="rounded"/></td>
<td class="tdcheckbox"><input type="checkbox" id="h24_1" name="h24_1" class="rounded"/></td>
<td class="tdcheckbox"><input type="checkbox" id="h27_1" name="h27_1" class="rounded"/></td>
<td class="tdcheckbox"><input type="checkbox" id="h30_1" name="h30_1" class="rounded"/></td>
<td><input type="hidden" name="checkedsizes_1" id="checkedsizes_1" value="0"></td>
</tr>
</table>
我想要做的是计算每行上选中的复选框的数量。保留我可以有动态的行数。这不必是实时的,就在提交之前,需要将隐藏字段 checkedsizes_1
设置为该行选中的复选框的数量。
可能有 5 行,因此需要循环遍历每个表行(1,2,3...)并对选中的复选框进行计数,然后使用结果填充相应的输入 checkedsizes_X
。
所以我的提交是:
<INPUT TYPE="button" VALUE="Continue" onClick="submitFunction()">
调用的函数是:
<SCRIPT>
function submitFunction() {
document.sales_order_details.action="/sales/new_autospread_range";
</SCRIPT>
所以我设想这样的事情:
<SCRIPT>
function submitFunction() {
countcheckboxperrow();
document.sales_order_details.action="/sales/new_autospread_range";
</SCRIPT>
<script>
countcheckboxperrow(){;
var checkboxes=0;
//foreach table row in table class="authors-list" count the checkedcheckboxes starting with h. [id^="h"] h being field name first letter
//set the corresponsing input in that field row that starts with
//repeat for next row id^="checkedsizes"]
</script>
提前感谢您的帮助,如果需要澄清,请告诉我。总之,需要一个脚本来计算该行中选中的复选框的数量,并使用该值填充隐藏字段。
再次感谢。
fiddle http://jsfiddle.net/emL6x/2/
最佳答案
我还没有测试过,但是像这样的 jQuery 应该可以做到
countChecks(){
$('.author-list tr').each(function(){
var count = 0;
var hdn = $(this).find('input[name^="checkedsizes"]');
count = $(this).find(':checkbox:checked').length;
hdn.val(count);
});
}
关于javascript/jquery 计算当前行中选中的复选框的数量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16147982/