漂亮的复选框 jQuery 选择器问题
我试图用 div 内的阻止链接替换经典的默认复选框 - 它将显示为按钮,通过单击它会切换类。实际上切换类是有效的,但我还需要一个解决方案,如何切换 checked
属性。请参阅下面
这是我的 HTML 代码
<div class="editor-field">
<div class="pretty-checkbox">
<input checked="checked" id="IsLegalFirm" name="IsLegalFirm" type="checkbox" value="true" />
<input name="IsLegalFirm" type="hidden" value="false" />
<a href="#" class="checkbox selected">IsLegalFirm </a>
</div>
</div>
<div class="editor-field">
<div class="pretty-checkbox">
<input checked="checked" id="IsTaxable" name="IsTaxable" type="checkbox" value="true" />
<input name="IsTaxable" type="hidden" value="false" />
<a href="#" class="checkbox selected">IsTaxable </a>
</div>
</div>
隐藏的 input
之所以存在,是因为 ASP.NET MVC 在使用 Html.Checkbox()
帮助程序时引入了它。
这是我的 JS:
<script type="text/javascript">
$(document).ready(function () {
$(".checkbox").click(
function (event) {
event.preventDefault();
$(this).toggleClass("selected");
// this needs a change
$(this).parent().find(":checkbox").attr("checked", "checked");
});
});
</script>
显然这只会打开 checked
但当它已经被选中时,它不会取消选中它。我相信它应该对两种输入都这样做。
有人可以帮助我找到正确的选择器/解决方案吗?
I am trying to replace the classic default checkbox with a blocked link inside a div - it would display as a button and by clicking on it it would toggle the class. Actually toggling the class works but I also need a solution how to toggle if the checked
attribute. See below
This is my HTML code
<div class="editor-field">
<div class="pretty-checkbox">
<input checked="checked" id="IsLegalFirm" name="IsLegalFirm" type="checkbox" value="true" />
<input name="IsLegalFirm" type="hidden" value="false" />
<a href="#" class="checkbox selected">IsLegalFirm </a>
</div>
</div>
<div class="editor-field">
<div class="pretty-checkbox">
<input checked="checked" id="IsTaxable" name="IsTaxable" type="checkbox" value="true" />
<input name="IsTaxable" type="hidden" value="false" />
<a href="#" class="checkbox selected">IsTaxable </a>
</div>
</div>
The hidden input
is there because ASP.NET MVC introduces it when using Html.Checkbox()
helper.
This is my JS:
<script type="text/javascript">
$(document).ready(function () {
$(".checkbox").click(
function (event) {
event.preventDefault();
$(this).toggleClass("selected");
// this needs a change
$(this).parent().find(":checkbox").attr("checked", "checked");
});
});
</script>
Obviously this only turns on checked
but when it's already checked it doesn't uncheck it. And I believe it should do it to both inputs.
Can someone help me with a right selector/solution?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
或者您可以只强制复选框的“click”事件,例如
realCheckbox.click()
Or you could maybe just force the "click" event for the checkbox, like
realCheckbox.click()
不确定链接的目的,但传统上使用标签和 for 属性将文本绑定到复选框。这是一个示例:
我留下了您的链接。但是,如果您不需要它们,只需删除并保留标签即可。这是一个 jsFiddle 供您使用: http://jsfiddle.net/bK3ju/1/ 。请注意,不需要 JavaScript 即可将标签连接到复选框。
鲍勃
Not certain the purpose of the links, but traditionally text is bound to a checkbox using a label and the for attribute. Here is an example:
I left your links in. However, if you don't need them just remove and leave the labels. Here is a jsFiddle for you to play with: http://jsfiddle.net/bK3ju/1/. Notice no javascript is needed to hook up the labels to the checkboxes.
Bob