简体   繁体   English

Checkbox onclick在IE8中不起作用,但在IE9中起作用

[英]Checkbox onclick is not working in IE8 but working in IE9

I'm creating dynamic checkbox and attaching onclick event with that. 我正在创建动态复选框,并附加onclick事件。 Below is the code: 下面是代码:

var table = document.getElementById(tableID);
var rowCount = table.rows.length;
var row = table.insertRow(rowCount);

var cell1 = row.insertCell(0);
var checkbox = document.createElement("input");
checkbox.type = 'checkbox';
checkbox.onclick = testClick();
cell1.appendChild(checkbox);

function testClick() {
            alert("Hello");
}

The above piece of code is working fine with IE9 but not with IE8. 上面的代码在IE9上可以正常工作,但在IE8上不能正常工作。 I'm using Jquery 1.7.1. 我正在使用Jquery 1.7.1。

Any help will be greatly appreciated. 任何帮助将不胜感激。 Thanks.. 谢谢..

您需要传递函数处理程序,而不是对onclick属性的函数调用:

checkbox.onclick = testClick;

If you are using jQuery, why not use it? 如果您使用的是jQuery,为什么不使用它呢? That takes care of cross browser issues 那可以解决跨浏览器的问题

$(domElement).click(function(){
    // do your thing here
});

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

相关问题 下拉无法在ie8和ie9中工作 - drop down not working in ie8 and ie9 JavaScript可在FF + IE9中运行,但不能在IE8中运行 - Javascript Working in FF + IE9 but not IE8 AJAX 请求在 IE8 和 IE9 中不起作用 - AJAX request not working in IE8 and IE9 带有Facebox的Onclick在IE8中不起作用 - Onclick with facebox not working in IE8 想要在选中复选框时显示特定的div,如果未选中则隐藏div。不起作用IE8 / IE9 - Want to display a particular div on checking a checkbox and if it is left unchecked then hide div .Not working IE8/IE9 javascript-Confirm()在IE9中不起作用,但在IE8中起作用 - javascript - confirm() not working in IE9 but working in IE8 AutoCompleteExtender OnClientItemSelected在IE8中不工作(在IE9中工作)[javascript子字符串] - AutoCompleteExtender OnClientItemSelected not working in IE8 (working in IE9) [javascript substring] Request.UrlReferrer在IE8中无法正常工作,在IE9中也可以正常工作 - Request.UrlReferrer is not working in IE8 and working fine IE9 为什么此JQuery代码不能在IE8和IE9上运行? - Why isn't this JQuery code working on IE8 and IE9? jQuery ajax成功方法是在ie8中工作而不是在ie9中工作? - jQuery ajax success method is working in ie8 but not in ie9?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM