简体   繁体   English

JavaScript:复选框onchange / onclick函数不起作用

[英]Javascript: checkbox onchange/onclick function not working

I have this function for my computation that whenever the text fields are populated, it will add a stack to a variable and whenever a checkbox is checked on the text field, the text field wont add a value. 我有此函数用于计算,只要填充文本字段,它将为变量添加堆栈,并且每当在文本字段上选中复选框时,文本字段都不会添加值。

everything is working except the checkbox function, I already tried onchange and onclick but nothing happens. 除复选框功能外,其他所有功能均正常运行,我已经尝试过onchangeonclick,但没有任何反应。 Can you check my codes? 你可以检查我的密码吗?

Script: 脚本:

function submission(){
    x = 19;
    ctr = 1;
    ctr2 = 0;
    days = ('<?php echo $query[0]->totaldays; ?>') - 1;
    $('#payment').html('');
    for(i = 1; i<=x ; i++){
        if($('#guest'+i).val() != ""){
          ctr++;
        }
        if(document.getElementById('kid'+i).checked){
          ctr2++;
        }
    }

    totalCount = 0;
    totalCount = parseInt(ctr) - parseInt(ctr2);
    totalCount = parseInt(ctr) * parseInt(days);
    totalCount = parseFloat(totalCount) * 100;
    $('#totalPayment').val(totalCount);
    $('#payment').html(totalCount);

}

HTML: HTML:

    <div class="row">
    <div class="col-sm-10">
        <label for="exampleInputtext1"><a style = "color:black; font-size: 10px;" ><input onchange="submission()" type="checkbox" name="kid1" id="kid1" > </a>Guest 1:</label>
    <input type="text" class="form-control" onblur="submission()"  name="guest1" id="guest1"/>
        </div>
    </div>

The onclick event handler will work for checkbox,you can try this code, HTML: onclick事件处理程序将适用于复选框,您可以尝试以下代码,HTML:

<input onclick="submission(this)" type="checkbox" name="kid1" id="kid1" >

JS: And you can check and see whether the checkbox is checked or not using the following code, JS:您可以使用以下代码检查并查看该复选框是否被选中,

function submission(click){
....
var clicked=click.checked;
...
}

if it is true that means the checkbox has been checked and if it is false it has not been checked. 如果为true,则表示该复选框已选中;如果为false,则未选中。

$("#guest1").change(function() {
if(this.checked) {
    //Do this.
}
else{
   // Do this.
}});
  • don't need to mention onclick function on html. 无需在html上提及onclick函数。 just add this code in JS. 只需在JS中添加此代码即可。
  • its calling auto based on id. 其根据ID的自动调用。

let me know if you need any help in understanding. 如果您需要任何帮助,请告诉我。

Ajay 阿杰

thanks for your answers, it seems like I just messed up a little bit on my mathematical equation. 感谢您的回答,似乎我只是弄乱了我的数学方程式。

totalCount = 0;
totalCount = parseInt(ctr) - parseInt(ctr2);
totalCount = parseFloat(totalCount) * parseInt(days);
totalCount = parseFloat(totalCount) * 100;
$('#totalPayment').val(totalCount);
$('#payment').html(totalCount);

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

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