最近我一直在使用Chrome插件API,我正在寻求开发一个插件,它将使我的网站管理工作变得更容易.

现在我想做的是在选中某个复选框时触发一个事件.

推荐答案

简而言之:使用change事件.这里有几个实际的例子.由于我误解了问题,我将包括jQuery示例和普通JavaScript.不过,使用jQuery不会获得太多好处.

单个复选框

使用querySelector.

var checkbox = document.querySelector("input[name=checkbox]");

checkbox.addEventListener('change', function() {
  if (this.checked) {
    console.log("Checkbox is checked..");
  } else {
    console.log("Checkbox is not checked..");
  }
});
<input type="checkbox" name="checkbox" />

单个复选框 with jQuery

$('input[name=checkbox]').change(function() {
  if ($(this).is(':checked')) {
    console.log("Checkbox is checked..")
  } else {
    console.log("Checkbox is not checked..")
  }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<input type="checkbox" name="checkbox" />

多个复选框

以下是复选框列表的示例.要 Select 多个元素,我们使用querySelectorAll而不是querySelector.然后使用Array.filterArray.map提取校验值.

// Select all checkboxes with the name 'settings' using querySelectorAll.
var checkboxes = document.querySelectorAll("input[type=checkbox][name=settings]");
let enabledSettings = []

/*
For IE11 support, replace arrow functions with normal functions and
use a polyfill for Array.forEach:
https://vanillajstoolkit.com/polyfills/arrayforeach/
*/

// Use Array.forEach to add an event listener to each checkbox.
checkboxes.forEach(function(checkbox) {
  checkbox.addEventListener('change', function() {
    enabledSettings = 
      Array.from(checkboxes) // Convert checkboxes to an array to use filter and map.
      .filter(i => i.checked) // Use Array.filter to remove unchecked checkboxes.
      .map(i => i.value) // Use Array.map to extract only the checkbox values from the array of objects.
      
    console.log(enabledSettings)
  })
});
<label>
   <input type="checkbox" name="settings" value="forcefield">
   Enable forcefield
</label>
<label>
  <input type="checkbox" name="settings" value="invisibilitycloak">
  Enable invisibility cloak
</label>
<label>
  <input type="checkbox" name="settings" value="warpspeed">
  Enable warp speed
</label>

多个复选框 with jQuery

let checkboxes = $("input[type=checkbox][name=settings]")
let enabledSettings = [];

// Attach a change event handler to the checkboxes.
checkboxes.change(function() {
  enabledSettings = checkboxes
    .filter(":checked") // Filter out unchecked boxes.
    .map(function() { // Extract values using jQuery map.
      return this.value;
    }) 
    .get() // Get array.
    
  console.log(enabledSettings);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<label>
   <input type="checkbox" name="settings" value="forcefield">
   Enable forcefield
</label>
<label>
  <input type="checkbox" name="settings" value="invisibilitycloak">
  Enable invisibility cloak
</label>
<label>
  <input type="checkbox" name="settings" value="warpspeed">
  Enable warp speed
</label>

Html相关问答推荐

为移动显示重新排序内容

简化指标与Delta保持一致

Chrome中是否有一个本地显示密码功能,用于`input type = password/`?""<"">

放大缩小标题在外面的图像

CURL邮箱中的图像不能调整其大小

Angular 15 p-对话框不使用组件 Select 器呈现HTML

为什么我的搜索栏会出现在WONG位置?

MatSnackBar: colored颜色 不起作用

当文本添加到元素中时,将元素拉到页面上

SVG';COLOR&39;属性不优先于通用css';COLOR&39;属性

如何用背景色填充边框半径创建的间隙

为什么 CSS 网格超出了父级?

我在桌面版 html css 代码上给出了位置 margin-left margin-top 标签,如何将其删除到手机上以使其响应?

如何为Vuetify输入控件减小标签和字段之间的间距?

如何使用 CSS 和 HTML 将文本放入图像中?

在 SQL 中合并 HTML 表中的单元格

每次按下按钮时减小字体大小的 jquery 函数

不理解 CSS 中的 General sibling combinator (~)

flex-box 中的图像在 Chrome 和 Firefox 中看起来不同 - 如何使它们在 Firefox 中看起来像?

具有淡入/淡出效果的 CSS 选框