复选框检查事件监听器 [英] Checkbox Check Event Listener

查看:322
本文介绍了复选框检查事件监听器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近我一直在使用Chrome插件API,我正在开发一个插件,这将使我更容易管理一个网站。



我希望做的是在某个复选框被选中时触发一个事件。
由于本网站不属于我,我无法更改代码,因此我使用的是Chrome API。其中一个主要问题是,不是存在ID,而是存在名称。

谢谢



 < input type =h2_lin>解决方案

复选框name =复选框>



没有jQuery



使用jQuery-如 querySelector

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

checkbox.addEventListener('change',function(){
if(this.checked){
// Checkbox is checked ..
} else {
//复选框未被选中。
}
});



使用jQuery



 < ($(this).is(':checked')){
// Checkbox($)$('input [name = checkbox]')。被检查。
} else {
//复选框未被选中。
}
});


Recently I have been working with the Chrome Plugin API and I am looking to develop a plugin which will make life easier for me for managing a website.

Now what I wish to do is to fire an event when a certain checkbox is checked. As this website does not belong to me I cannot change the code therefore I am using the Chrome API. One of the main problems is that rather than there being an ID, there is a Name. I was wondering if I could fire the function once the certain checkbox with the 'name' is checked.

Thanks

解决方案

Assuming you're using this markup:

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

Without jQuery

Using the jQuery-like querySelector.

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

checkbox.addEventListener( 'change', function() {
    if(this.checked) {
        // Checkbox is checked..
    } else {
        // Checkbox is not checked..
    }
});

With jQuery

$('input[name=checkbox]').change(function(){
    if($(this).is(':checked')) {
        // Checkbox is checked..
    } else {
        // Checkbox is not checked..
    }
});

这篇关于复选框检查事件监听器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆