gpt4 book ai didi

javascript - jQuery 查找复选框并选中/取消选中它

转载 作者:行者123 更新时间:2023-12-02 17:51:22 24 4
gpt4 key购买 nike

我在循环中创建的复选框:

@foreach (L1 item in Model)
{
<li><label class="checkbox"><input type="checkbox" id="@(item.Name)"
class="clink" value="@(item.Name)"/>@item.ForeName</label></li>
}

然后我使用 jQuery 来检查它:

 $('#cworks').prop('checked', true);

上面的方法不起作用。这是使用 firebug 生成的 html

<label class="checkbox"><input id="cworks" class="clink" value="cworks" type="checkbox">cworks</label>

最佳答案

在您的示例中,您设置了一个类 class="checkbox" ,然后在 jquery 调用中使用 id #checkbox input ,以便使其正常工作,您应该这样做:

$('.checkbox input[id="somestring"]').prop('checked', true);

然后,如果您的 item.Name 设置了类似 var item.Name = my_prefix_somestring 的前缀,则可以使用通配符。

 $('.checkbox input[id=*"my_prefix"]').prop('checked', true);

如果 item.Name 是唯一的(我认为),那么只需执行以下操作:

$('#Careworks_Map').prop('checked', true);

编辑

根据您的更新,您应该这样做:

$('#cworks').prop('checked', true);

http://jsfiddle.net/daguru/7FUBn/2/

关于javascript - jQuery 查找复选框并选中/取消选中它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21335933/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com