[英]Jquery Checkbox change event not firing
Am trying to sortout this small issue from past hour : 我正在尝试解决过去一个小时的这个小问题:
<script type="text/javascript">
$(document).ready(function(){
$("input[name='isClubMember']:checkbox").mousedown(function() {
if (!$(this).is(':checked')) {
this.checked = confirm("Are you sure?");
$(this).trigger("change");
}
});
});
</script>
Funnily this works in JSFiddle ..but not in my jsp page. 有趣的是,这在JSFiddle ..中有效,但在我的jsp页面中无效。 Sample 样品
The above code gives me null
error at this line (in Debug console) 上面的代码使我在此行出现null
错误(在调试控制台中)
$("input[name='isClubMember']:checkbox").mousedown(function() {
JQ -Ver : 1.7.2 Browser : IE8 JQ -Ver:1.7.2浏览器:IE8
Update : Error in IE console : 更新 :IE控制台中的错误:
'null' is null or not an object
at the above mentioned line 在上述行
It looks like your JSP page includes the Prototype library after jQuery. 看来您的JSP页面在jQuery之后包含Prototype库。
Prototype's $() function takes an id and returns null
if it cannot find the element, which is consistent with the behavior you're observing. 原型的$()函数带有一个id,如果找不到该元素,则返回null
,这与您观察到的行为一致。
Try using jQuery
instead of $
everywhere in your code: 尝试在代码中的所有位置使用jQuery
而不是$
:
jQuery("input[name='isClubMember']:checkbox").mousedown(function() {
// ...
});
Or put your code in a closure that is passed the jQuery
object in a $
argument: 或者将您的代码放在$
参数中传递jQuery
对象的闭包中:
(function($) {
// The rest of your code...
})(jQuery);
Or take advantage of the fact that a ready
handler is passed the same $
argument: 或采取的事实,即一个ready
处理程序通过相同的$
参数:
jQuery(document).ready(function($) {
// The code in your 'ready' handler...
});
You may also want to run jQuery in noConflict mode, to avoid overriding Prototype's $()
function if the order of inclusion changes. 您可能还想在noConflict模式下运行jQuery,以避免在包含顺序更改时重写Prototype的$()
函数。
i would suggest, you could add a class name to the checkbox, and find the checkbox by a class name! 我建议,您可以在复选框中添加一个类名,然后通过一个类名找到该复选框! it seems thats the problem with ie8 in this case! 在这种情况下,似乎多数民众赞成在ie8的问题!
I think in your page jQuery is not connected. 我认为您的页面中jQuery未连接。 Check. 校验。
I think so because $ - function and returns object anyway, but if there is no jQuery then $(str) returns null. 我认为是因为$-无论如何都会返回对象,但是如果没有jQuery,则$(str)返回null。
我猜选择器在IE8中坏了,尝试[type ='checkbox'],无论如何应该更快
$("input[name='isClubMember'][type='checkbox']").mousedown(function() {
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.