动态填充复选框并动态设置其检查状态 [英] Populating checkboxes dynamically and set their checked state dynamically

查看:65
本文介绍了动态填充复选框并动态设置其检查状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个for循环,它可以动态地连续生成7个复选框,如下所示:

I have a for loop that generates 7 checkboxes in a row dynamically as follows :

@for (int i = 1; k < order.Rows.length; i++) {
Row: @i
    <ul>
        @for (int j = 1; j < order.NumCheckboxes.length; j++)
        {

           <li>
                <input type="checkbox" id="check-@j" checked>
                <label for="check-@j>/label>
           </li>
          }
    </ul>
}

然后我有另一个循环,其中包含要设置为选中状态的复选框的值. 复选框状态循环

Then I have another loop that contains the values of the checkboxes to be set checked. Checkbox state loop

@for(int k = 0; k < order.CheckboxChecked.length; k++) 
{
}

此循环包含代表复选框状态(3,5)的值.

This loop contains the values that represents the checkboxes state (3, 5).

例如,在第1行中,连续动态生成7个复选框,并选中第3个和第5个复选框.循环复选框状态设置在第一行中选中的第3个和第5个复选框.如果在第2行中选中了第一个复选框,则循环Checkbox状态将在第二行中设置选中的第一个复选框,依此类推.

For example, in Row 1, 7 checkboxes are dynamically generated in a row and the 3rd and 5th checkboxes are checked. The loop Checkbox state sets the 3rd and 5th checkbox checked in the first row. And if in Row 2, the first checkbox is checked, the loop Checkbox state sets the 1st checkbox checked in the second row and so on.

有人可以建议我如何实现这一目标吗?

Can someone please advise me on how to achieve this ?

推荐答案

如果仅使用一个元素,则始终可以修改HTMLInputElement的.checked属性:

If you're working with just one element, you can always just modify the HTMLInputElement's .checked property:

$('.myCheckbox')[0].checked = true;
$('.myCheckbox')[0].checked = false;

使用.prop()和.attr()方法代替此方法的好处是它们将对所有匹配的元素进行操作.

The benefit to using the .prop() and .attr() methods instead of this is that they will operate on all matched elements.

希望这对您有帮助...

Hope this helps...

这篇关于动态填充复选框并动态设置其检查状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆