请帮我解决这个问题。
我将获取驻留在 html 表中的已选中 checkbox 数组的索引号,但每次我都会提醒 .each 内的每个 checkbox 索引() ,每次循环都会返回 0。
但是如果我删除表格及其所有 tr 和 td (保留两个复选框),此代码将起作用
我的 jquery 代码:
function notify() {
$("input[type = 'checkbox']:checked").each(function(){
alert($(this).index()); /*always return zero when the check[] inside a table or div */
});
}
function main() {
$("input[type = 'button']").click(notify);
}
$(document).ready(main);
我的 html 代码:
<table>
<tr><td>
<input type='checkbox' name='check[]' />
</td><td>
<input type='checkbox' name='check[]' />
</td> </tr>
</table>
<input type='button' name='button' value='ok'/>
最佳答案
试试这个
$("input:checkbox:checked").each(function(index,value){
alert($(this).parents('td').index());
});
关于javascript - 当表或 div 中的复选框数组时,.each() 内的 .index() 始终返回 0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16096860/