Javascript错误,更新复选框/按钮基于其他复选框的结果

时间:2012-07-17 15:35:53

标签: javascript

首先快速描述一下:

我有4个表(1和3包含一行复选框,2和4包含单选按钮)。

我要做的就是让它在我选择表1中的复选框时,通过将等效复选框设置为“禁用”来更新表2,3,4。

我认为最简单的选择是使用'VALUE'属性,因为这在每个表上都是相同的(例如,第一个表上VALUE 4的字段相当于其他表上VALUE 4的字段)。

我的表每个表都有一个唯一的ID,并且共享一个类名为“TableClassName”。 它们看起来都类似于:

<table id="TableID1" class="TableClassName">
<tr>
<th class="CellGrey">1</td>
<th class="CellGrey ">2</td>
<th class="CellGrey ">3</td>
<th class="CellGrey ">4</td>
</tr>
<tr>
<td class="CellRed "><INPUT TYPE="checkbox" NAME="ignore[]" VALUE="1" checked></td>
<td class="CellWhite "><INPUT TYPE="checkbox" NAME="ignore[]" VALUE="2"></td>
<td class="CellWhite "><INPUT TYPE="checkbox" NAME="ignore[]" VALUE="3"></td>
<td class="CellWhite "><INPUT TYPE="checkbox" NAME="ignore[]" VALUE="4"></td>
</tr>
</table>

到目前为止我所拥有的是:

script type="text/javascript">
$(document).ready(function()
{
$("#TableID1").on('click','input:checkbox',function()
        { 
            if ($(this).attr('checked')) 
                {
                var $val = $(this).attr('value');
                //alert($val);
                $(".DBSelectTable td").function()
                    {
                        if ($(this).child().attr('VALUE') == $val)
                            {
                                $(this).child().attr('disabled');
                            } 
                    }

            }       
        }
    )
}       
);
</script>

但是,我收到了错误:

  

“未捕获类型错误:对象[对象对象]没有方法'功能'”

我的理论很简单:

  1. 选择#TableID1表

  2. 找出第一张表中的哪个方框已被点击并获取VALUE属性

  3. 选择.TableClassName表

  4. 检查是否有相同的值,并添加“已禁用”属性

  5. 但是出了点问题,我无法从这里看到哪里。任何帮助,将不胜感激。

1 个答案:

答案 0 :(得分:0)

请注意,如果是attr,我会使用prop。 另外,您可以比较find函数中的值。

$(document).ready(function()
{
$("#TableID1").on('change','input:checkbox',function()
        { 
            if ($(this).prop('checked')) 
                {
                    var val = $(this).val();                
                    $(".DBSelectTable td").find("[value='"+ val +"']").prop('disabled',true)
                }       
        }
    )
}       
);