2

请帮我解决这个问题。

我将获取驻留在 html 表中的选中复选框数组的索引号,但每次我提醒.each() 内的每个复选框的索引时,它都会为每个循环返回 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'/>

这是我在 jsfiddle 中的代码的链接

4

2 回答 2

4

尝试这个

$("input:checkbox:checked").each(function(index,value){
          alert($(this).parents('td').index());  
  }); 

http://jsfiddle.net/cuNE6/9/

于 2013-04-19T03:45:23.247 回答
0

这可能是您正在寻找的。既然您将复选框包装在 td 中,为什么不找到 td 的索引

[演示]

function notify() {

    $("input[type = 'checkbox']:checked").each(function () {
        alert($(this).parents('td').index());
    });
}
于 2013-04-20T03:36:13.190 回答