【问题标题】:How can I change the switch display based on the value from the database如何根据数据库中的值更改开关显示
【发布时间】:2020-01-06 22:45:18
【问题描述】:

所以我在一周中的每一天都有一个开关,简单的 html 和 css。当开关被点击到开/关位置时,它会将事件发送到 jquery,然后将其发送到 PHP 以更新数据库。然后我查询该数据库以获取每天的值(1 表示开启,0 表示关闭)。

如何根据 PHP 中的 1 或 0 值更改开关的显示。例如:如果数据库中的值为 1,则应该显示 ::after css(绿色)。如果它是 0,那么 ::before 应该显示(灰色)。当然,用户可以通过单击开关简单地更改它。

这里是开关的代码:

if($row['monday'] == '1'){ 
    echo'<div class="material-switch" style="margin-left:45%;">
       <input id="mondaySelect" name="mondaySelect" type="checkbox"/>
       <label for="mondaySelect" class="label-success"></label>
    </div>';                    
}

Here is the CSS for the switch, the switch when selected changes to on position and green color, when clicked again goes grey to the off position.

.material-switch > input[type="checkbox"] {
    display: none;   
}

.material-switch > label {
    cursor: pointer;
    height: 0px;
    position: relative; 
    width: 40px;  
}

.material-switch > label::before {
    background: rgb(0, 0, 0);
    box-shadow: inset 0px 0px 10px rgba(0, 0, 0, 0.5);
    border-radius: 8px;
    content: '';
    height: 16px;
    margin-top: -8px;
    position:absolute;
    opacity: 0.3;
    transition: all 0.4s ease-in-out;
    width: 40px;
}
.material-switch > label::after {
    background: rgb(255, 255, 255);
    border-radius: 16px;
    box-shadow: 0px 0px 5px rgba(0, 0, 0, 0.3);
    content: '';
    height: 24px;
    left: -4px;
    margin-top: -8px;
    position: absolute;
    top: -4px;
    transition: all 0.3s ease-in-out;
    width: 24px;
}
.material-switch > input[type="checkbox"]:checked + label::before {
    background: inherit;
    opacity: 0.5;
}
.material-switch > input[type="checkbox"]:checked + label::after {
    background: inherit;
    left: 20px;
}

为了清楚起见,这是一张图片

【问题讨论】:

  • 当前在打开或关闭开关时如何工作?你如何分配合适的班级?
  • @NawedKhan 开关只是用css打开和关闭,这里的所有代码都是开关打开或关闭的代码...它只是一个复选框,我使用jquery检查如果复选框被选中,则将该值发送到 php.ini。所有这些都很好,只需要知道从数据库中获取值后如何更改 CSS。因此,当复选框未选中时,有没有办法将框的 CSS 更改为 ::before 或 ::after 代码。
  • 如果对应的值是1,就在输入中加入checked。developer.mozilla.org/en-US/docs/Web/HTML/Element/input/…
  • @NawedKhan,是的,这行得通。哈哈,这完全是我的想法。过多考虑 CSS 中的 ::before 和 ::after。 :) 谢谢

标签: php jquery css


【解决方案1】:

这个CSS 取决于复选框的checked 属性。如果它被选中,它将显示开关为打开,否则它将显示开关为关闭,这是一个很好的CSS 设计。

我们可以简单地在 PHP 中使用它,如果数据库中的值为 1,则只需回显 "checked" 属性,否则不要回显“已检查”。

例如改变这个

if($row['monday'] == '1'){ 
    echo'<div class="material-switch" style="margin-left:45%;">
       <input id="mondaySelect" name="mondaySelect" type="checkbox"/>
       <label for="mondaySelect" class="label-success"></label>
    </div>';                    
}

到这里

if($row['monday'] == '1')$mondayChecked = "checked";
else $mondayChecked = "";

echo'<div class="material-switch" style="margin-left:45%;">
   <input id="mondaySelect" name="mondaySelect" type="checkbox" ' 
   . $mondayChecked .' />
   <label for="mondaySelect" class="label-success"></label>
</div>';  

【讨论】:

  • 就这么简单。感谢您的帮助,这绝对有效。赞赏。
  • 不客气,是的,这很简单,因为它的 CSS 很好。
【解决方案2】:

如果你想使用材质切换,可能值得实现https://material.io/develop/web/components/input-controls/switches/

您可以将代码附加到您的模板中并检查它是否有效。

<!-- Required styles for MDC Web -->
<link rel="stylesheet" href="https://unpkg.com/material-components-web@latest/dist/material-components-web.min.css">

if($row['monday'] == '1'){ 
    echo'<div class="mdc-switch mdc-switch--checked">
  <div class="mdc-switch__track"></div>
  <div class="mdc-switch__thumb-underlay">
    <div class="mdc-switch__thumb">
      <input type="checkbox" id="basic-switch" class="mdc-switch__native-control" role="switch" checked>
    </div>
  </div>
</div>';                    
}

【讨论】:

  • 我检查了它,但我认为 Accountants 解决方案非常简单,并创建了我正在寻找的结果。谢谢
猜你喜欢
  • 1970-01-01
  • 2019-03-10
  • 2021-01-12
  • 1970-01-01
  • 2021-12-21
  • 1970-01-01
  • 1970-01-01
  • 2015-11-02
  • 1970-01-01
相关资源
最近更新 更多