![](/img/trans.png)
[英]checked checkbox value shown in input textbox and uncheck will another place
[英]jQuery checked checkbox value shown in input textbox and uncheck will remove
我一直在尝试找到标题中所述的解决方案,其中当您有多个复选框并且当您选中带有诸如“ blood”之类的值的复选框时,将添加到输入文本框中。 文本框中的每个选中值都将以空格或逗号分隔。 取消选中复选框时,与之对应的值将从文本框中删除。
到目前为止,我遇到的最接近的示例就是以下示例: 按单击顺序在文本框中显示复选框值
我试图通过使用<input type="text" name="text" id="results" value="" />
并具有$li.text(this.value);
到$li.val(this.value);
但什么也没出现。 不幸的是,jQuery不是我的专长。
如果可能的话,有人可以说一下吗?
也许这就是您所需要的?
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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.