[英]Change event not firing for checkbox in Chrome extension
I am creating a chrome extension where I am dynamically creating the checkbox.我正在创建一个 chrome 扩展,我在其中动态创建复选框。 Now in document ready I have placed the code to handle the "checked" change event of checkbox but the code is not working as expected because am not getting any alerts when i click it.现在文档准备好了,我已经放置了代码来处理复选框的“选中”更改事件,但是代码没有按预期工作,因为当我单击它时没有收到任何警报。
Creating dynamic checkbox:创建动态复选框:
function createExtensionUI(extensionListArray){
var bkg = chrome.extension.getBackgroundPage();
var extensionLength = extensionListArray.length;
for(var i=0; i<extensionLength; i++){
var extensionObject = extensionListArray[i];
if(extensionObject.type == "extension"){
var checkbox = '<label><input type="checkbox" class= "extensionbox" id= ' + extensionObject.id + '/>'+extensionObject.shortName+'</label>';
$('#extensionList').append(checkbox);
bkg.console.log(checkbox);
}
}
}
dynamic checkbox html code sample:动态复选框html代码示例:
<label><input type="checkbox" class= "extensionbox" id= "llegcghmokaemodgdddnchiijfdbfnlg"/>Coremetrics Bar for Chrome</label>
Code for change event:更改事件代码:
$(document).ready(function(){
chrome.management.getAll(getExtensionList);
$("input[type=checkbox]").change(function() {
alert('hello');
});
});
Am not getting any "hello" alert.没有收到任何“你好”警报。 My UI for extension is coming properly.我的扩展用户界面正在正常运行。
I have tried the following but they also didn't worked:我尝试了以下方法,但它们也没有奏效:
$("input[type=checkbox]").change
$(".extensionbox").change
I am using jquery version 1.11.1
我正在使用 jquery 版本1.11.1
I believe the problem lies in asynchronous execution.我认为问题在于异步执行。 Hard to say for sure without your getExtensionList
code though.但是,如果没有您的getExtensionList
代码,就很难确定。
You apparently create your elements from getExtensionList
, which gets executed after the rest of $(document).ready
, so your listeners do not attach because DOM elements don't exist yet.您显然是从getExtensionList
创建您的元素,它在$(document).ready
的其余部分之后执行,因此您的侦听器不会附加,因为 DOM 元素尚不存在。
To fix, move the code that attaches listeners to the end of createExtensionUI
, it belongs there logically anyway.要修复,请将附加侦听器的代码移动到createExtensionUI
的末尾,它无论如何都属于逻辑。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.