如何在复选框更改时运行函数?
我正在尝试在此基础上编写小复选框替换函数 - 但我做错了。
代码:
(function($) {
$.fn.checking = function() {
if ($('.checbox').prop('checked', false) === true) {
$('.new-checkbox').prop('id', 'unchecked');
} else {
$('.new-checkbox').prop('id', 'checked');
}
};
})(jQuery);
$(document).ready(function() {
$('.checkbox').wrap('<div class="checkbox-wrapper"></div>');
$('.checkbox-wrapper').append('<p class="new-checkbox">Blue = Checked, Red = Unchecked</p>');
$('.checkbox').checking();
$('.checbox').bind('change', function() {
$(this).checking();
});
});
Playground :LIVE DEMO
最佳答案
这是你想要的吗?我的印象是你把事情过于复杂化了。
$('input[type="checkbox"]').change(function() {
alert ("The element with id " + this.id + " changed.");
});
关于jquery - 观察输入的变化[类型 ="checkbox"],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6571948/