javascript - 事件监听器未在 Chrome 扩展中的 options.js 中触发

标签 javascript jquery google-chrome google-chrome-extension

在我的选项页面中,在我的 Chrome 扩展程序中,我想在用户输入值并单击提交按钮时读取用户输入。

options.html

<html>
<head>

  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js"></script>

</head>
<body>

<h1 style="font-family:Raleway;font-weight:200;text-align:center;font-size:50px;color:white;">Themes</h1>

<input type="number" id="day" value="2" class="day">
<button id = "button1">Submit</button>

  <script src="options.js"></script>
</body>
</html>

options.js

...
document.getElementById("button1").addEventListener("change", function(e) {
    var day = document.getElementById("day").value; // get the current value of the input field.
    alert(day);
});

最佳答案

您正在监听更改而不是点击

addEventListener("click", function(){})

当前,您仅在按钮值更改时运行函数(并且由于按钮没有值,因此永远不会触发)

关于javascript - 事件监听器未在 Chrome 扩展中的 options.js 中触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37213554/

相关文章:

javascript - 使用javascript解析字符串

javascript - Jquery仅获取Bootstrap中选定的项目

jquery - 如何使用 JQ 删除 CSS 类

javascript - 重定向到数据 : 时 chrome.webRequest 跨源不允许方案

javascript - 在 Chrome 扩展程序中使用 PAC 文件重定向 HTTPS 请求似乎失败

javascript - 服务注入(inject)中的 AngularJS $inject 错误

javascript - Array.prototype.indexOf.call 的问题

CSS图像缩小

javascript - 我如何将 id 分配给 svg 元素? (给raphaeljs吧)

javascript - 在滚动上添加和删除类