简体   繁体   English

while循环中的复选框不会产生价值

[英]Checkboxes in while loop won't give value

I'm trying to get all the values of my selected checkboxes into a textarea. 我正在尝试将所选复选框的所有值都放入一个textarea中。 The checkboxes and their values are taken from a DB. 复选框及其值取自数据库。 I don't know why, but the only thing my textarea is giving me is the word 'on' when something is selected. 我不知道为什么,但是我的文本区域唯一给我的是选择某项时的单词“ on”。

these are the checkboxes: 这些是复选框:

 <div class="controls" id="c_b">
        <?php
            echo "<ul>";
            while($user = $allUsers->fetch_assoc())
            {
                echo "<li><input type='checkbox'> " . $user['username'] . " </input></li>";
            }
            echo "</ul>";
            ?>
            <br />
            </div>

and this is the function: 这是函数:

function updateTextArea() {         
     var allVals = [];
     $('#c_b :checked').each(function() {
       allVals.push($(this).val());
     });
     $('#t').val(allVals)
  }
 $(function() {
   $('#c_b input').click(updateTextArea);
   updateTextArea();
 });

thanks in advance, I hope somebody can see what I'm missing! 预先感谢,希望有人能看到我所缺少的! Jana 亚娜

If you don't set a value on a checkbox, it will default to on if checked. 如果未在复选框上设置值,则如果选中,则默认为on You need to set the value to each username. 您需要为每个用户名设置值。

<input type="checkbox" value="bob" />

您可以将属性值添加到复选框: http : //jsfiddle.net/RNdFS/

You can try 你可以试试

     $('#c_b input[type=checkbox]:checked').each(function() {
       allVals.push($(this).val());
     });

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM