简体   繁体   English

获取复选框总计数并在文本框中保存值(如果选中)

[英]Get Checkbox total count & save value in textbox if checked

I have written below code to get a total count of Checked chechbox and save the value checked value comma separated in textbox. 我已经编写了以下代码,以获取Checked chechbox的总数,并保存在文本框中分隔的值checked逗号。

HTML: HTML:

<label class='checkbox-inline'><input type='checkbox' class='checkbox-btn' value='1' onchange='checkCount(this.value);'></label>
<label class='checkbox-inline'><input type='checkbox' class='checkbox-btn' value='2' onchange='checkCount(this.value);'></label>
<label class='checkbox-inline'><input type='checkbox' class='checkbox-btn' value='3' onchange='checkCount(this.value);'></label>

<input type="text" name="proId" id="proId">

JS: JS:

function checkCount(elm) {
  var cheCount = $(":checkbox:checked").length;
  document.getElementById('selectCount').innerHTML = cheCount;

  definVal = document.getElementById('proId').value ;
  var inval = elm+","+definVal;
  document.getElementById('proId').value = inval;

  if(cheCount==0){
    document.getElementById('proId').value = "";
  }
}

Check the output in below image: 检查下图中的输出: 在此处输入图片说明

My issue is that, when i unchecked the checkbox, its value is adding in textbox instead of getting remove. 我的问题是,当我取消选中该复选框时,其值是在文本框中添加而不是删除。

Make below changes, just call js method dont send its value 进行以下更改,仅调用js方法不发送其值

And then check by their classname 然后按他们的类名检查

 function checkCount(elm) { var checkboxes = document.getElementsByClassName("checkbox-btn"); var selected = []; for (var i = 0; i < checkboxes.length; ++i) { if(checkboxes[i].checked){ selected.push(checkboxes[i].value); } } document.getElementById("proId").value = selected.join(); document.getElementById("total").innerHTML = selected.length; } 
 <label class='checkbox-inline'><input type='checkbox' class='checkbox-btn' value='1' onchange='checkCount();'></label> <label class='checkbox-inline'><input type='checkbox' class='checkbox-btn' value='2' onchange='checkCount();'></label> <label class='checkbox-inline'><input type='checkbox' class='checkbox-btn' value='3' onchange='checkCount();'></label> <input type="text" name="proId" id="proId"> <div>Total Selected : <span id="total">0</span></div> 

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

相关问题 添加在文本框中选中的总计复选框 - Add the total checkbox checked in the textbox 如何计算在下一个td文本框中动态创建td具有相同值的复选框的计数? - How to get count of checkbox checked which has same value in next td textbox dynamically created td? 计算值为 A 的复选框总数 - count total checked checkboxes with value A 当复选框被选中或复选框未选中删除值时,如何在另一个文本框中获得复选框值和文本框值的总和 - how can i get sum of checkbox value and textbox value in another textbox when checkbox is checked or if checkbox unchecked remove the value 获取选中复选框的值? - Get the value of checked checkbox? 获取复选框选中值 - Get checkbox checked value 仅在选中复选框时显示文本框,如果未选中,则不将字段保存在数据库中 - Appear a textbox only when the checkbox is checked and not save the field in the database if is not checked 如何添加显示选中的复选框的总值 - How to add a display a total value of the checkbox that are checked 将复选框的选中状态链接到文本框的值 - Linking checkbox's checked state to a value of a textbox 单击文本框中的复选框,获取复选框的计数和值,如果未选中,则应删除该值,并应减少文本框中的计数 - Get count and values of checkbox on click of it in the textbox and on uncheck the value should be removed and count should decreased in textbox
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM