从选中的复选框中获取所有值,并将其放入文本区域
Getting all the value from a checked checkbox and put into a textarea
我想通过javascript将我的所有复选框值放入文本区域,一旦取消选中,它就会从数组中弹出。。。这是代码:
<form enctype="multipart/form-data" method="get" id="frmMain" name="frmMain">
<input type = "checkbox" id = "chk1" value = "CheckBox1" onclick ='checkTickArea(this.id)'>
<input type = "checkbox" id = "chk2" value = "CheckBox2" onclick = 'checkTickArea(this.id)'>
<input type = "checkbox" id = "chk3" value = "CheckBox3" onclick = 'checkTickArea(this.id)'>
<input type = "checkbox" id = "chk4" value = "CheckBox4" onclick = 'checkTickArea(this.id)'>
<input type = "checkbox" id = "chk5" value = "CheckBox5" onclick = 'checkTickArea(this.id)'>
<textarea id= "taSignOff" style="width:180px;height:90px;font-size:20px; resize: none;" rows="3" readonly> </textarea>
</form>
javascript:
var selectedvalue=[];
function checkTickArea(id)
{
$('#'+id).change(function () {
//If checked then push the value
if( $('#'+id).is(":checked"))
{
selectedvalue.push($('#'+id).attr("value"));
}else
{
//This is what pops the value from the array when the checkbox is unchecked.
/* var index = selectedvalue.indexOf($('#'+id).attr("value"));
if (index > -1) {
selectedvalue.splice(index, 1);
}*/
selectedvalue.splice(selectedvalue.indexOf($(this).attr("value")),1);
}
document.getElementById('taSignOff').value = selectedvalue.join("->'n");
});
}
此外,此功能仅适用于firefox。。。但在Chrome和IE中,它不起作用。。。
这对我有效,尽管我不知道为什么你不在文本区域使用jQuery:jsFiddle
HTML:
<form enctype="multipart/form-data" method="get" id="frmMain" name="frmMain">
<input type = "checkbox" id = "chk1" value = "CheckBox1" />
<input type = "checkbox" id = "chk2" value = "CheckBox2" />
<input type = "checkbox" id = "chk3" value = "CheckBox3" />
<input type = "checkbox" id = "chk4" value = "CheckBox4" />
<input type = "checkbox" id = "chk5" value = "CheckBox5" />
<textarea id= "taSignOff" style="width:180px;height:90px;font-size:20px; resize: none;" rows="3" readonly> </textarea>
</form>
JavaScript:
var selectedvalue = [];
$('input[type="checkbox"]').on('change', checkTickArea);
function checkTickArea() {
//If checked then push the value
if ($(this).is(":checked")) {
selectedvalue.push($(this).val());
} else {
selectedvalue.splice(selectedvalue.indexOf($(this).val()), 1);
}
// Why not use jQuery tho?
//$('taSignOff').val(selectedvalue.join("->'n"));
document.getElementById('taSignOff').value = selectedvalue.join("->'n");
}
删除内联javascript并执行:
var elems = $('[id^="chk"]');
elems.on('change', function() {
$('#taSignOff').val(
elems.filter(':checked').map(function() {
return this.value;
}).get().join("->'n")
);
});
FIDDLE
相关文章:
- onkeyup无法动态创建多个文本区域
- 具有所有样式的文本正在复制到可编辑文本区域
- 在文本区域POST后解码JSON
- HTML文本区域文本到XML
- 阻止在文本区域/文本框中输入数字
- 使用 javascript 单击图片后更改<文本区域>文本
- 使用 javascript 检测文本区域/文本输入中的 HTML 标记,但它应该支持常见的>和<
- 在按 Tab 键时选择文本区域文本
- 切换文本区域文本,但保留对文本的更改
- 清除文本区域/文本框内容的按钮
- 加载页面时,聚焦并全选文本区域文本
- 如何使可编辑文本区域文本不可选择
- 我的文本区域文本是't用javascript将我想要的样式设置为CSS
- 使用jQuery将文本区域文本安全地存储在隐藏字段中,然后稍后在PHP中使用该文本
- 删除文本区域文本提交在新的选项卡
- 如何在添加新文本时保持文本区域文本静态
- 使用 jQuery 将文本区域文本设置为格式正确的 HTML 代码
- 为什么文本添加到区域文本是通过页面刷新,而不是直接刷新
- 使用 jquery 修改文本区域文本
- 如何使用Javascript在文本区域(文本框)中单击(光标闪烁)而不使用鼠标单击