admin管理员组文章数量:1022695
I have a checkbox with a button, i want when user check the check box and click on the button show an alert and when checkbox is unchecked and user clicks on the button do something else.
I did something but i need help to acplish it. Any help greatly appreciated
$(document).ready(function(e) {
$('#remember').click(function() {
if (this.checked) {
alert("This is checked and you cliked");
} else {
alert("This is unchecked and you cliked");
}
});
});
JSFIDDLE
I have a checkbox with a button, i want when user check the check box and click on the button show an alert and when checkbox is unchecked and user clicks on the button do something else.
I did something but i need help to acplish it. Any help greatly appreciated
$(document).ready(function(e) {
$('#remember').click(function() {
if (this.checked) {
alert("This is checked and you cliked");
} else {
alert("This is unchecked and you cliked");
}
});
});
JSFIDDLE
Share Improve this question asked Sep 19, 2014 at 4:47 AmirAmir 5372 gold badges8 silver badges25 bronze badges 1- 1 try replacing (this.checked) with ($('input[type="checkbox"]').is(':checked')). – Harish Ambady Commented Sep 19, 2014 at 4:55
4 Answers
Reset to default 4You are showing alerts in Checkbox's onclick. You would need to add the code in button's onClick.
See updated fiddle
$(document).ready(function(e) {
$('#buttonId').click(function() {
if ($('#remember').is(':checked')) {
alert("This is checked and you cliked");
} else {
alert("This is unchecked and you cliked");
}
});
});
Hope this helps.
You can use jquery .is() selector for this:-
$('input[type="button"]').click(function() {
if ($('#remember').is(':checked')) {
alert("This is checked and you cliked");
} else {
alert("This is unchecked and you cliked");
}
});
You can set id or class to button
$(document).ready(function(e) {
$('#btnEnter').click(function() {
if ($('#remember').is(':checked')) {
alert("This is checked and you cliked");
} else {
alert("This is unchecked and you cliked");
}
});
});
Demo
use Prop();
$('[type=button]').click(function () {
if ($("#remember").prop("checked")) {
alert("This is checked and you cliked");
} else {
alert("This is unchecked and you cliked");
}
});
DEMO
$(document).ready(function(e) {
$('#buttonId').click(function() {
if ($('#remember').prop('checked')) {
alert("This is checked and you cliked");
} else {
alert("This is unchecked and you cliked");
}
});
});
I have a checkbox with a button, i want when user check the check box and click on the button show an alert and when checkbox is unchecked and user clicks on the button do something else.
I did something but i need help to acplish it. Any help greatly appreciated
$(document).ready(function(e) {
$('#remember').click(function() {
if (this.checked) {
alert("This is checked and you cliked");
} else {
alert("This is unchecked and you cliked");
}
});
});
JSFIDDLE
I have a checkbox with a button, i want when user check the check box and click on the button show an alert and when checkbox is unchecked and user clicks on the button do something else.
I did something but i need help to acplish it. Any help greatly appreciated
$(document).ready(function(e) {
$('#remember').click(function() {
if (this.checked) {
alert("This is checked and you cliked");
} else {
alert("This is unchecked and you cliked");
}
});
});
JSFIDDLE
Share Improve this question asked Sep 19, 2014 at 4:47 AmirAmir 5372 gold badges8 silver badges25 bronze badges 1- 1 try replacing (this.checked) with ($('input[type="checkbox"]').is(':checked')). – Harish Ambady Commented Sep 19, 2014 at 4:55
4 Answers
Reset to default 4You are showing alerts in Checkbox's onclick. You would need to add the code in button's onClick.
See updated fiddle
$(document).ready(function(e) {
$('#buttonId').click(function() {
if ($('#remember').is(':checked')) {
alert("This is checked and you cliked");
} else {
alert("This is unchecked and you cliked");
}
});
});
Hope this helps.
You can use jquery .is() selector for this:-
$('input[type="button"]').click(function() {
if ($('#remember').is(':checked')) {
alert("This is checked and you cliked");
} else {
alert("This is unchecked and you cliked");
}
});
You can set id or class to button
$(document).ready(function(e) {
$('#btnEnter').click(function() {
if ($('#remember').is(':checked')) {
alert("This is checked and you cliked");
} else {
alert("This is unchecked and you cliked");
}
});
});
Demo
use Prop();
$('[type=button]').click(function () {
if ($("#remember").prop("checked")) {
alert("This is checked and you cliked");
} else {
alert("This is unchecked and you cliked");
}
});
DEMO
$(document).ready(function(e) {
$('#buttonId').click(function() {
if ($('#remember').prop('checked')) {
alert("This is checked and you cliked");
} else {
alert("This is unchecked and you cliked");
}
});
});
本文标签: javascriptIf checkbox is checked and user clicks on the button do thisStack Overflow
版权声明:本文标题:javascript - If checkbox is checked and user clicks on the button do this - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745560611a2156140.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论