简体   繁体   中英

Alert when checkbox is ticked by user

I have a checkbox in a website that is not controlled by me.But it can be modified by javascript,html,jquery..etc(they have provided web applet for doing so)

Threre is a checkbox which can be identified with an ID 'Zcb_1'. All i wasnt if the user checks the checkbox only then an alert should come. W hen the user navigates to the page. the checkbox might be ticked or unticked, but i dont care then.

I need this alert only when the state of the checkbox changes from unticked to ticked. I am not much into jquery and ajax, so if this can be done done simply( imean just using javascript)it would be great.

EDIT: How can the same be impletmentd if I have a dropdown(Select) with values 'Y' , 'N'and blank and then alert should come when the 'Y' is selected by user.(Alert when the value is changed from blank or 'N' to 'Y'

As you say you can use jQuery (I also suppose what you ask is technically feasible ), this would be the code:

$(function(){
   $("#your_checkbox_id").change(function(){
       if($(this).is(":checked")){
          alert("Checkbox checked!!");
       }
   });
});

Cheers

Just do:

document.getElementById("IDHERE").onchange = function() {
    if (!this.checked)
        alert("Unchecked");
}

Maybe this is what you want:

if (document.getElementById('CheckBox1').checked){
   alert("Checkbox ticked");
}
else{
    alert("Checkbox not ticked");
}

您可以使用标准的javascript onclick方法,然后检查以查看this.checked,然后调用程序。

Using javascript:-

<script type="text/javascript">  
//This is done to make the following JavaScript code compatible to XHTML. <![CDATA[ 
function verifybox() 
{ 
if (document.myform.checkbox1.checked) 
{ 
alert('This box is checked.'); 
} 
else 
{ 
alert('This box is not checked.'); 
} 
} 
//]]>  
</script>  
<form name="myform" action="#">  
<input type="checkbox" name="checkbox1" />Check here  
<p>  
<input type="button" value="Verify Box" name="Mybutton"  
onclick="verifybox()" /></p>  
</form>  

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM