多个复选框值将显示在文本框中 [英] Multiple checkbox value will show in textbox

查看:251
本文介绍了多个复选框值将显示在文本框中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码:

<body>
<div align="center">
<b>A<input type="checkbox" name="a" id="check" value="a"></b>
<b>B<input type="checkbox" name="b" id="check" value="a"></b>
<b>B<input type="checkbox" name="c" id="check" value="c"></b>
<b>D<input type="checkbox" name="d" id="check" value="d"></b>
</div>
<table align="center">
<tr>
<td>Text:</td>
<td><input type="text" name="text" id="text"></td>
</tr>
</table>
</body>

我正在尝试:如果(多个)复选框被选中(或选中)该值将被分配到abcd或acd或bd之类的复选框。为此我写了jQuery

I'm trying that: if (more than one) checkbox is selected (or checked) that value will be assigned into the checkbox like "abcd" or "acd" or "bd".For that I have written jQuery

<script type="text/javascript">
$(document).click(function(){
if($("#check").attr('checked')){
    $("#text").val(("#check").val());
}
});
</script>

能够一次打印一个txtbox值,但无法将所有选中的值放入文本框中一次。
我哪里错了?任何输入都会受到赞赏。

able to print one txtbox value at a time,but not able to put all checked value in the textbox at a time. Where I am going wrong ??Any inputs will appreciated.

推荐答案

我可能没有正确理解你,但是这个小提琴对你有帮助吗? http://jsfiddle.net/XwGJ9/1/

I may have not understood you correctly, but does this fiddle help you? http://jsfiddle.net/XwGJ9/1/

更改是javascript:

The change is the javascript:

var $textInput = $('#text');
var $checkBox = $('#checkboxes');

$('input').click(function(){
    populateTextInput();
});

function populateTextInput () {
    // empty text input
    $textInput.val('');

    // print out all checked inputs
    $checkBox.find('input:checked').each(function() {
        $textInput.val( $textInput.val() + $(this).val() );
    });
}

编辑:更新

这篇关于多个复选框值将显示在文本框中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆