在div或p中显示已选中的复选框

时间:2015-09-09 07:47:15

标签: javascript jquery html css

我想在div中显示已选中的复选框

例如

<input id="checkbox1" type="checkbox"><label for="checkbox1">Checkbox 1</label>

<p>you have selected:</p> <div>checkbox 1</div>

将有多个复选框(37),我需要它才能选中所有复选框

我觉得这很有意义

由于

2 个答案:

答案 0 :(得分:6)

您可以使用:

$(":checkbox").change(function(){
   $('div').html($(':checked').map(function(){
      return $(this).next().text()
   }).get().join(','));
});

<强> Working Demo

答案 1 :(得分:1)

我就是这样做的:

$("input[type='checkbox']").change(function(){
    $("#checked").empty();
    $("input[type='checkbox']:checked").each(function(){
        $("#checked").html($("#checked").html() + $(this).next().text() + "<br/>");
    });
});

Here is the JSFiddle demo

相关问题