简体   繁体   English

如何在复选框的onclick上调用2个函数?

[英]How to call 2 functions on onclick of a checkbox?

I have a checkbox, which when clicked some operations are performed. 我有一个复选框,单击该复选框会执行一些操作。 I am not gonna specify what operation it really performs...for the time being let's think some mathematical operations (simple addition/ multiplication) are performed when I check that checkbox. 我不会指定它真正执行的操作...暂时让我想一想,当我选中该复选框时,会执行一些数学运算(简单的加法/乘法)。

<input type="checkbox" name="chk_ceil" id="chk_ceil" onclick="thecheckfunction();"> What the Check

So, on onclick property of the checkbox, I call a JavaScript function. 因此,在复选框的onclick属性上,我调用了JavaScript函数。

Function definition: 功能定义:

<script>
    function thecheckfunction()
    {
    // SOme operations (operation A & B performed)    
    alert('checkbox is checked');
    }
</script>

And now comes the crazy part. 现在是疯狂的部分。 When I uncheck the checkbox, the function is called again & the operations are performed once again, which I don't wanna happen as it is not supposed to perform an operation when I uncheck the checkbox. 当我取消选中该复选框时,该函数会再次调用并再次执行该操作,这是我不想发生的,因为当我取消选中该复选框时,它不应该执行操作。 I thought there must be a way to tackle this issue. 我认为一定有办法解决这个问题。 Maybe by calling two functions on the onclick property of the checkbox. 也许通过在复选框的onclick属性上调用两个函数。 One when the checkbox is checked & one when checkbox is unchecked. 一次选中复选框,一次选中未选中。

Help??? 救命???

Use as 用于

 function thecheckfunction(){
    var chk_ceil= document.getElementById("chk_ceil");       
    if(chk_ceil.checked == true){
         alert('checkbox is checked');
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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