I have a checkbox on a form which is unchecked by default as usual. now I want to perform two separated actions on checked and unchecked state of this checkbox.
this is my checkbox:
syn
and this is my script:
function doalert(id){
if(this.checked) {
alert('checked');
}else{
alert('unchecked');
}
}
it just alerts uncheched!! what is the best way to do that.
解决方案
We can do this using JavaScript, no need of jQuery. Just pass the changed element and let JavaScript handle it.
HTML
syn
JS
function doalert(checkboxElem) {
if (checkboxElem.checked) {
alert ("hi");
} else {
alert ("bye");
}
}