按钮复选框不保存已选中

时间:2022-03-13 09:48:35

I am using bootstrap, and am trying to convert checkboxes into a button that shows when it is pressed or not(checked or not). If I keep the code showing the checkboxes, it works perfect. If I keep the code as labelled buttons, the "checked" buttons do not load properly(they do save).

我正在使用bootstrap,我正在尝试将复选框转换为一个按钮,该按钮显示何时按下(检查与否)。如果我保持代码显示复选框,它就完美了。如果我将代码保留为带标签的按钮,则“已选中”按钮无法正确加载(它们会保存)。

Solved : It turns out that I just needed to echo out "btn-group active" for the already active checkboxes.

解决:事实证明,我只需要为已经有效的复选框回显“btn-group active”。

Checkbox Code w/checkboxes(That works)

复选框代码带复选框(可行)

<div class="btn-group" id="salesman" data-toggle="buttons"></div>
<h5>Salesman :</h5>
<?php
        $salesman = json_decode($invoice['Invoice']['salesman'], true);
        $salesman_names = array(1 => "User1",2 => "User2",3 => "User3",4 => "User4",5 => "User5");
            foreach ($salesman_names AS $i => $name) {
                if ($salesman[$name] == "checked") {
                    echo '<label class="btn btn-default"><input type="checkbox" name="data-invoice-salesman[]" value="'.$i.'" checked/>&nbsp;&nbsp;'.$name.'</label>';
                } else {
                    echo '<label class="btn btn-default"><input type="checkbox" name="data-invoice-salesman[]" value="'.$i.'" />&nbsp;&nbsp;'.$name.'</label>';
                }
            }       
        ?>

Checkbox Code w/out Checkboxes(that needs fixed?)

复选框代码没有复选框(需要修复?)

   <div class="btn-group" id="salesman" data-toggle="buttons">
<h5>Salesman :</h5>
<?php
        $salesman = json_decode($invoice['Invoice']['salesman'], true);
        $salesman_names = array(1 => "User1",2 => "User2",3 => "User3",4 => "User4",5 => "User5");
            foreach ($salesman_names AS $i => $name) {
                if ($salesman[$name] == "checked") {
                    echo '<label class="btn btn-default"><input type="checkbox" name="data-invoice-salesman[]" value="'.$i.'" checked/>&nbsp;&nbsp;'.$name.'</label>';
                } else {
                    echo '<label class="btn btn-default"><input type="checkbox" name="data-invoice-salesman[]" value="'.$i.'" />&nbsp;&nbsp;'.$name.'</label>';
                }
            }       
        ?>

2 个解决方案

#1


1  

I think you search a toggle buttons, check example bellow.

我想你搜索一个切换按钮,查看下面的示例。

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link href="https://netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://netdna.bootstrapcdn.com/bootstrap/3.0.0/js/bootstrap.min.js"></script>

<div class="btn-group" data-toggle="buttons">
  <label class="btn btn-default active">
    <input type="checkbox" checked autocomplete="off"> Checkbox 1 (pre-checked)
  </label>
  <label class="btn btn-default">
    <input type="checkbox" autocomplete="off"> Checkbox 2
  </label>
  <label class="btn btn-default">
    <input type="checkbox" autocomplete="off"> Checkbox 3
  </label>
</div>

#2


0  

From what I gather you want to link the clicking of a label to the toggling of a checkbox (which could be hidden).

从我收集的内容中,您想要将标签的点击链接到复选框的切换(可以隐藏)。

<label for="me">
    Blah1
    <input type="checkbox" id="me">
</label>

#1


1  

I think you search a toggle buttons, check example bellow.

我想你搜索一个切换按钮,查看下面的示例。

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link href="https://netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://netdna.bootstrapcdn.com/bootstrap/3.0.0/js/bootstrap.min.js"></script>

<div class="btn-group" data-toggle="buttons">
  <label class="btn btn-default active">
    <input type="checkbox" checked autocomplete="off"> Checkbox 1 (pre-checked)
  </label>
  <label class="btn btn-default">
    <input type="checkbox" autocomplete="off"> Checkbox 2
  </label>
  <label class="btn btn-default">
    <input type="checkbox" autocomplete="off"> Checkbox 3
  </label>
</div>

#2


0  

From what I gather you want to link the clicking of a label to the toggling of a checkbox (which could be hidden).

从我收集的内容中,您想要将标签的点击链接到复选框的切换(可以隐藏)。

<label for="me">
    Blah1
    <input type="checkbox" id="me">
</label>