简体   繁体   English

jQuery在输入文本框中显示了选中的复选框值,并且取消选中将删除

[英]jQuery checked checkbox value shown in input textbox and uncheck will remove

I've been trying to find the solution as said in my title where when you have multiple checkboxes and when you check a checkbox with the value such as "blood" will be added to an input textbox. 我一直在尝试找到标题中所述的解决方案,其中当您有多个复选框并且当您选中带有诸如“ blood”之类的值的复选框时,将添加到输入文本框中。 Each checked value in the textbox will be separated either with space or a comma. 文本框中的每个选中值都将以空格或逗号分隔。 When you uncheck a checkbox the value corresponding to it will be removed from the textbox. 取消选中复选框时,与之对应的值将从文本框中删除。

So far the closest example i've came across is from this one: Display checkbox value in textbox in the order of click 到目前为止,我遇到的最接近的示例就是以下示例: 按单击顺序在文本框中显示复选框值

I've tried to change the code around a bit by using <input type="text" name="text" id="results" value="" /> and having $li.text(this.value); 我试图通过使用<input type="text" name="text" id="results" value="" />并具有$li.text(this.value); to $li.val(this.value); $li.val(this.value); but nothing appears. 但什么也没出现。 Unfortunately jQuery isn't what I specialize in. 不幸的是,jQuery不是我的专长。

If possible can someone please shed some light? 如果可能的话,有人可以说一下吗?

Maybe this is what you need? 也许就是您所需要的?

var arr = [];
$('#inputs input').change(function() {
  if (this.checked) {
    arr.push(this.value);
  }
  else {
   arr.splice(arr.indexOf(this.value), 1);
  }
  $('#target').val(arr + '');
});

<!DOCTYPE html>
<html>
<head>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<meta charset=utf-8 />
<title>JS Bin</title>
</head>

<body>
  <div id="inputs">
    <input type="checkbox" value="Apple">
    <input type="checkbox" value="Orange">
    <input type="checkbox" value="Pineapple">
    <input type="checkbox" value="Mango">
  </div>
  <input type="text" id="target"/>
</body>
</html>

Try this 尝试这个

Fiddle demo 小提琴演示

here you just need to apply a click event on all your check boxes. 在这里,您只需要在所有复选框上应用click事件。 and fill the input box on every click. 并在每次点击时填写输入框。

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

相关问题 在输入文本框中显示选中的复选框值,然后取消选中另一个位置 - checked checkbox value shown in input textbox and uncheck will another place 如果选中jquery,请取消选中复选框 - Uncheck checkbox if checked with jquery 当输入字段(文本框)具有值时,选中/取消选中复选框 - Check/uncheck checkbox when input field (textbox) has a value jQuery检查复选框是否已选中,并使用复选框的值更新texbox的值,并取消选中是否为空 - jQuery check if checkbox is checked and update the value of the texbox with the checkbox's value and uncheck if it's empty 如果其他使用jquery选中,如何取消选中复选框 - How to uncheck checkbox if other checked using jquery 设置取消选中jQuery复选框的值 - Set the value of uncheck jquery checkbox 如果选中复选框,则为getJSON,然后取消选中 - getJSON if checkbox is checked then uncheck 当复选框被选中或复选框未选中删除值时,如何在另一个文本框中获得复选框值和文本框值的总和 - 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 如果选中,则jQuery获取复选框值,并在取消选中时删除值 - jQuery get checkbox value if checked and remove value when unchecked jQuery动态添加禁用和取消选中输入复选框 - jquery dynamically add input checkbox with disabled and uncheck
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM