简体   繁体   English

如何使用javascript / jQuery检查/取消选中HTML CheckBoxFor

[英]How to check/uncheck HTML CheckBoxFor with javascript/jQuery

I've looked at all the other answers to the question "How to check/uncheck a checkbox with Javascript/jQuery" and they seem to say to use: 我已经查看了问题“如何使用Javascript / jQuery选中/取消选中复选框”的所有其他答案,它们似乎说使用:

$("#idSelector").prop('checked', true);
$("#idSelector").prop('checked', false);

However, since the razor html helper adds a second input element to the mix, this creates complications. 但是,由于razor html helper将第二个输入元素添加到混合中,因此会带来复杂性。

 $(document).on("click", "input[type='radio']", function() { $("input[type='radio']").each(function() { var that = this; $(this).parent().siblings("#checkBoxArray").find(":checkbox").each(function(i, item) { $(item).prop('checked', that.checked); }); }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> <div style="margin-bottom: 50px;"> <div style="clear: both;"></div> <div style="margin: 25px; margin-top: 15px; margin-bottom: 10px;"> <div id="CheckBoxRow" style="border-bottom: 1px solid lightgrey; padding-bottom: 10px;"> <label> <input id="holeSizes" name="holeSizes" type="radio" value="4" data-bind="checked:holeSizes">Choice Group One</label> <div id="checkBoxArray" style="min-height: 18px;"> <div> <input class="Checkbox1" data-val="true" data-val-required="required." id="one" name="one" type="checkbox" value="true" data-bind="checked:one"> <input name="one" type="hidden" value="false" data-bind="value:one">One</div> <div> <input class="Checkbox1" data-val="true" data-val-required="required." id="two" name="two" type="checkbox" value="true" data-bind="checked:two"> <input name="two" type="hidden" value="false" data-bind="value:two">Two</div> <div> <input class="Checkbox1" data-val="true" data-val-required="required." id="three" name="three" type="checkbox" value="true" data-bind="checked:three"> <input name="three" type="hidden" value="true" data-bind="value:three">Three</div> <div> <input class="Checkbox1" data-val="true" data-val-required="required." id="four" name="four" type="checkbox" value="true" data-bind="checked:four"> <input name="four" type="hidden" value="false" data-bind="value:four">Four</div> <div> <input class="Checkbox1" data-val="true" data-val-required="required." id="five" name="five" type="checkbox" value="true" data-bind="checked:five"> <input name="five" type="hidden" value="false" data-bind="value:five">Five</div> <div> <input class="Checkbox1" data-val="true" data-val-required="required." id="six" name="six" type="checkbox" value="true" data-bind="checked:six"> <input name="six" type="hidden" value="false" data-bind="value:six">Six</div> <div> <input class="Checkbox1" data-val="true" data-val-required="required." id="seven" name="seven" type="checkbox" value="true" data-bind="checked:seven"> <input name="seven" type="hidden" value="false" data-bind="value:seven">Seven</div> </div> </div> </div> </div> 

I have used this code to change the checkbox selection status based on which radio button is clicked and the checkboxes show a check as if they were selected, but the underlying value of the hidden input box is not changed to reflect this. 我已使用此代码根据单击的单选按钮更改了复选框的选择状态,并且复选框显示了好像已被选中的复选框,但是隐藏输入框的基础值并未更改以反映这一点。

What code do I need to use to change both the visual checkbox and the hidden value? 我需要使用什么代码来更改可视复选框和隐藏值?

Update 更新

I've almost got it figured out. 我几乎想通了。 Kind of. 的种类。 These checkboxes are in a Kendo template which seems to be automatically applying a Knockout binding to each checkbox. 这些复选框位于Kendo模板中,该模板似乎自动将Knockout绑定应用于每个复选框。

data-bind="checked:one"

So changing the visual appearance of the box and the value of the hidden input by itself still isn't working completely. 因此,更改框的外观和隐藏输入本身的值仍无法完全起作用。 I added in another line to change the value of the data-binding, but this is still one step too short. 我添加了另一行来更改数据绑定的值,但这仍然太短了一步。 I need to change the value of the model that the knockout binds to. 我需要更改击倒绑定到的模型的值。

Is there an easy way to change the value of a data-bound model? 有没有简单的方法来更改数据绑定模型的值?

If your code looks like: 如果您的代码如下所示:

@using(Html.BeginForm......
{
...
@Html.LabelFor(s=>s.idSelector, "idSelector")
@Html.CheckBoxFor(s=>s.idSelector)
...
}

only what you have to do for set checkbox as 'checked' is: 只需将复选框设置为“已选中”即可:

$("#idSelector").prop('checked', true);

This code is working for to change the checked status of checkbox to false if true. 此代码适用于将复选框的选中状态更改为false(如果为true)。

if ($("#cbName").is(":checked")) {
            $("#cbName").click();
        }

While this may not help the poster due to the age of this question, I hope this will help any that find this searching like I did, specifically for the Kendo().CheckBoxFor while editing a grid item. 尽管由于这个问题的年代久远,这可能对海报没有帮助,但我希望这对像我一样找到此搜索的人有所帮助,尤其是在编辑网格项目时针对Kendo()。CheckBoxFor。 I was able to check/uncheck the way listeeeek mentioned but due to the under the hood stuff in kendo, the model it was bound to was not updating. 我可以检查/取消选中listeeeek提到的方式,但是由于剑道中的底层内容,它绑定的模型没有更新。 I added the second line here to update the model 我在这里添加了第二行以更新模型

$("#CHECKBOXNAME").prop('checked', desiredBool);
$('#GRIDNAME').data().kendoGrid.editable.options.model.CHECKBOXITEM = desiredBool;

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM