【问题标题】:jQuery and checkboxes, i can't see the changesjQuery 和复选框,我看不到更改
【发布时间】:2015-12-26 04:10:23
【问题描述】:

我在 ASP 项目中继器中有许多复选框:

<asp:Repeater ID="rpPostes" runat="server" DataSource="<%# this.ItemsPostesBudgetaires %>">

中继器中的某处,一个复选框,描述为:

<asp:CheckBox runat="server" Class="chk" ID="chkPieceJust" Text="Recues" Visible="<%#  Mode.HasFlag(ModeType._BoutonsApprobation)  %>" Enabled="<%# CheckBoxDispo() %>" Checked="<%# CheckBoxChecked(Container.DataItem as Transaction) %>" />

最后,一个按钮可以让每个复选框都变为真:

<input id="chkAllPieceJust" type="button" value="Toutes Pièces Justificatives Recues" onclick="CTCC.Transactions.PieceRecuClick(this);" />

最后这是我的 jQuery 函数:

CTCC.Transactions.PieceRecuClick = function (source) {

$('.chk').attr('checked', true);

}

点击按钮后发生的就是那种 html 输出:

<span class="chk" checked="checked"><input ... blablabla

但是什么都没有发生,复选框根本没有被选中...

我想不出我还需要什么...

【问题讨论】:

  • 始终使用.prop() 设置checked 属性。请参阅the documentation 中的属性与属性部分
  • 考虑到该文档, attr 现在对我来说完全没用......

标签: javascript jquery html asp.net checkbox


【解决方案1】:

Checkbox 控件的CssClass 属性实际上将类应用于包装复选框本身的span 元素。您可以通过在代码隐藏中分配该类直接将其应用于复选框元素,如下所示:

MyCheckBox.InputAttributes["class"] = "chk";

或者修改你的jquery来寻找.chk元素的输入子元素,像这样

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

【讨论】:

  • 一个完美的完整答案,解释得很好,我无话可说,它奏效了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-10-25
  • 1970-01-01
  • 1970-01-01
  • 2013-02-23
  • 1970-01-01
  • 1970-01-01
  • 2016-08-13
相关资源
最近更新 更多