更改复选框未触发
Checkbox on change is not firing
我在 html 页面上有一个复选框:
<input type="checkbox" id="chk-info" name="chk-info" />
当我尝试编写更改事件时 运行,它没有被触发。相反,如果我在它正常触发的功能之前放置一条警告消息。
alert('blah');
var sth = function(){
function m(){
$("input[type='checkbox']").click(function (e) {
if ($(this).is(':checked')) {
alert("true");
} else {
alert("false");
}
});
}
return{ m:m};
};
我也尝试过使用 id 选择器,但运气不佳。
谁能帮我指出问题出在哪里?
编辑:当我将复选框选中的功能放在上述功能之外时,它与附加在 <input>
标签上的 'onchange' 事件配合得很好
在页面加载时放置复选框的 onclick 事件。
$(document).ready(function () {
$("input[type='checkbox']").click(function (e) {
if ($(this).is(':checked')) {
alert("true");
} else {
alert("false");
}
});
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<input type="checkbox" id="chk-info" name="chk-info" />
<label for="chk-info">label</label>
$(document).ready(function () {
$("input[type='checkbox']").click(function (e) {
if ($(this).is(':checked')) {
$(this).val("true");
} else {
$(this).val("false");
}
});
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<input type="checkbox" id="chk-info" name="chk-info" />
<label for="chk-info">label</label>
我在 html 页面上有一个复选框:
<input type="checkbox" id="chk-info" name="chk-info" />
当我尝试编写更改事件时 运行,它没有被触发。相反,如果我在它正常触发的功能之前放置一条警告消息。
alert('blah');
var sth = function(){
function m(){
$("input[type='checkbox']").click(function (e) {
if ($(this).is(':checked')) {
alert("true");
} else {
alert("false");
}
});
}
return{ m:m};
};
我也尝试过使用 id 选择器,但运气不佳。 谁能帮我指出问题出在哪里?
编辑:当我将复选框选中的功能放在上述功能之外时,它与附加在 <input>
标签上的 'onchange' 事件配合得很好
在页面加载时放置复选框的 onclick 事件。
$(document).ready(function () {
$("input[type='checkbox']").click(function (e) {
if ($(this).is(':checked')) {
alert("true");
} else {
alert("false");
}
});
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<input type="checkbox" id="chk-info" name="chk-info" />
<label for="chk-info">label</label>
$(document).ready(function () {
$("input[type='checkbox']").click(function (e) {
if ($(this).is(':checked')) {
$(this).val("true");
} else {
$(this).val("false");
}
});
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<input type="checkbox" id="chk-info" name="chk-info" />
<label for="chk-info">label</label>