【问题标题】:Setting Bootstrap-Switch checkbox checked state with PHP or jQuery使用 PHP 或 jQuery 设置 Bootstrap-Switch 复选框选中状态
【发布时间】:2017-09-09 11:56:01
【问题描述】:

我需要根据 mysql 数据库中的值使用引导开关设置复选框的状态。

我正在尝试像这样使用 PHP,但它似乎不起作用,代码是摘录:

while ($row = $result->fetch_assoc()) {

    $checkdevicestate = $row["devicestatus"];
    if ($checkdevicestate == "false") {
        $devstatus = "";
    } else if ($checkdevicestate == "true") {
        $devicestatus = "checked";
    }   

    $devicecontent .= '<tr><td style="width:60%"><span data-icon="7" class="linea-icon linea-basic fa-fw"></span>'. $row["devicename"] .'</td><td style="width:100%"><input type="checkbox" id="' . $row["devicecode"].'" '.$devicestatus.' class="devicebtn"data-on-color="info" data-size="small"data-off-color="danger"></td></tr>';
}

开关检查状态似乎是随机的,而不是基于我从数据库接收到的值。

【问题讨论】:

    标签: php jquery twitter-bootstrap bootstrap-switch


    【解决方案1】:

    代码看起来不错,我只需将比较运算符修复为=== 以确定类型。

    while ($row = $result->fetch_assoc()) {
        $checkdevicestate = $row["devicestatus"];
        if ($checkdevicestate === "false") {
            $devstatus = "";
        } else if ($checkdevicestate === "true") {
            $devicestatus = "checked";
        }   
    
        $devicecontent .= '<tr><td style="width:60%"><span data-icon="7" class="linea-icon linea-basic fa-fw"></span>'. $row["devicename"] .'</td><td style="width:100%"><input type="checkbox" id="' . $row["devicecode"].'" '.$devicestatus.' class="devicebtn"data-on-color="info" data-size="small"data-off-color="danger"></td></tr>';
    }
    

    如果您在$row["devicestatus"] 中没有任何奇怪的状态,您甚至可以只使用ternary operator

    $devicestatus = ($checkdevicestate === "true") ? "checked='checked'" : '';
    

    【讨论】:

    • 谢谢!我根据您的建议修改了我的代码。感谢您的帮助:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-21
    • 2016-12-13
    • 1970-01-01
    • 2010-09-30
    相关资源
    最近更新 更多