【问题标题】:When using CheckBoxFor in MVC3, how can I use JQuery to only select the CheckBox INPUT?在 MVC3 中使用 CheckBoxFor 时,如何使用 JQuery 仅选择 CheckBox INPUT?
【发布时间】:2011-11-12 04:07:33
【问题描述】:

CheckBoxFor 呈现一个同名的隐藏字段 (and for good reason)

我需要在页面加载时根据 CheckBox 的值启用/禁用其他一些 INPUT 字段。但是,我无法正确使用 JQuery 选择器。我的函数最终触发了两次,因为选择器同时获取了 CheckBox 和 Hidden 输入字段。

这是我尝试过的,但都不起作用:

$('input[name$="Selected",type=checkbox]').each(function (){
   alert('here ' + this.name);
   EnableOrderItemDropDowns(this);
});

$('input[name$="Selected"],:checkbox').each(function (){
   alert('here ' + this.name);
   EnableOrderItemDropDowns(this);
});

这些方法都不起作用。后一种方法仍然不会仅过滤到 CheckBox 输入,因此对 EnableOrderItemDropDowns 的调用仍然会触发两次。

【问题讨论】:

  • 再一次,解决方案在我发布到 StackOverflow 后立即呈现给我。哦,好吧。 ($('input[name$="Selected"]:not(:hidden)'))

标签: jquery asp.net-mvc-3


【解决方案1】:
$('input[name$="Selected"][type="checkbox"]')

【讨论】:

    【解决方案2】:

    生成的隐藏字段不包含ID

    所以你可以这样做:

    $("#Selected")
    

    您将拥有的唯一元素是复选框

    【讨论】:

      猜你喜欢
      • 2018-08-22
      • 2015-04-13
      • 1970-01-01
      • 1970-01-01
      • 2016-05-28
      • 2016-11-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多