django复选框由jquery选中 [英] django checkbox select all by jquery
问题描述
我想选中所有复选框,当我点击顶部的检查,
以下是我的代码:
I want to select all checkbox when i click the top check, below is my code:
<script language="JavaScript" src="media/js/jquery-1.4.2.min.js"></script>
<script language="javascript">
$("#selectAll").change(function() {
$(".xxx:checkbox").attr('checked', this.checked);
});
</script>
Select All: <input type="checkbox" id="selectAll"><br />
<br />
One: <input type="checkbox" class="xxx" /><br />
Two: <input type="checkbox" class="xxx" /><br />
Three: <input type="checkbox" class="xxx" />
但为什么它不起作用?
推荐答案
因为 selectAll
在< script>
之前不存在跑。所以 $(#selectAll)
不匹配元素。 (jQuery不会在您将操作应用于任何元素时发出警告,只是默认无效。)
Because selectAll
does not exist at the time the <script>
is run. So $("#selectAll")
matches no elements. (jQuery doesn't warn you when you apply an operation to no elements, it just silently does nothing.)
将< script>
< input>
下的绑定,或将绑定放在$ code $(document).ready(function() ...}); 块,使代码在页面加载时运行。
Put the <script>
below the <input>
, or put the binding in a $(document).ready(function() { ... });
block to make the code run at page load time.
Aside:我会避免使用非标准jQuery选择器,如:checkbox
,尽可能的,因为它们强制使用JavaScript Sizzle选择器库而不是快速本机 querySelectorAll
。 input.xxx [type = checkbox]
将是另一种说法。
Aside: I would avoid use of the non-standard jQuery selectors like :checkbox
wherever possible, as they force the use of the JavaScript Sizzle selector library instead of fast native querySelectorAll
. input.xxx[type=checkbox]
would be another way of saying it.
这篇关于django复选框由jquery选中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!