[英]how to select only one checkbox using jquery?
I am using checkbox in the asp.net gridview. 我在asp.net gridview中使用复选框。 I want select only one checkbox at time.
我只想一次选择一个复选框。 if I select one checkbox other checkboxes should be deselected.
如果我选择一个复选框,则应取消选择其他复选框。
I have html view source 我有html查看源代码
<input id="ctl00_MainContent_mGrid_ob_mGridBodyContainer_ctl02_ctl02_ctl00_ChkID" type="checkbox"
<input id="ctl00_MainContent_mGrid_ob_mGridBodyContainer_ctl03_ctl02_ctl00_ChkID" type="checkbox"
Use a radio button instead of a checkbox. 使用单选按钮而不是复选框。
<form>
<input type="radio" name="parents" value="Mom" /> Mom<br />
<input type="radio" name="parents" value="Dad" /> Dad
</form>
Usually radio buttons are used for the exclusive functionality where only one item in a group is selected and the browser will do taht for you automatically with the right HTML. 通常,单选按钮用于独占功能,其中仅选择一个组中的一项,浏览器将使用正确的HTML自动为您处理。
For checkboxes, you could code it with jQuery like this: 对于复选框,可以使用jQuery进行编码,如下所示:
<div class="checkboxContainer">
<input id="ctl00_MainContent_mGrid_ob_mGridBodyContainer_ctl02_ctl02_ctl00_ChkID" type="checkbox"> Item 1<br>
<input id="ctl00_MainContent_mGrid_ob_mGridBodyContainer_ctl03_ctl02_ctl00_ChkID" type="checkbox"> Item 2<br>
<input id="ctl00_MainContent_mGrid_ob_mGridBodyContainer_ctl04_ctl02_ctl00_ChkID" type="checkbox"> Item 3
</div>
$("input[type='checkbox']").change(function() {
$(this).closest(".checkboxContainer").find("input[type='checkbox']").not(this).prop("checked", false);
$(this).prop("checked", true);
});
Working Demo: http://jsfiddle.net/jfriend00/hWEXx/ 工作演示: http : //jsfiddle.net/jfriend00/hWEXx/
Here's the pure HTML way of doing exclusive radio buttons (also in that same demo): 这是做排他单选按钮的纯HTML方法(也在同一演示中):
<div class="radioGroup">
<input type="radio" name="group1">Item A<br>
<input type="radio" name="group1">Item B<br>
<input type="radio" name="group1">Item C<br>
</div>
in asp.net you should use radiobuttons instead checkboxes (in this case) 在asp.net中,您应该使用单选按钮代替复选框(在这种情况下)
<div class="group">
<asp:RadioButton Id="radio1" runat="server" GroupName="radioGroup" />
<asp:RadioButton Id="radio2" runat="server" GroupName="radioGroup" />
</div>
GroupName
attribute make this functionality you need. GroupName
属性使您需要此功能。 (If radio1 is Checked
the radio2 is automatically unchecked. hope this help:) (如果
Checked
radio1,则将自动取消选中radio2。希望获得此帮助:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.