Bootstrap 开关 class 未应用于复选框输入?

Bootstrap switch class isn't applying on checkbox input?

我试图在复选框输入上应用 bootstrap 开关功能,但 Bootstrap 开关 class 没有应用在我的复选框输入上。

JS渲染bootstrap切换class:

cx.common.admin.tableSwitchableColumn('status', {
    editable: true,
    createdCell: function (td, cellData, rowData, row, col){
        $(td).data('status-id', rowData.id);
    },
    onText: 'Enable',
    offText: 'Disable'
})

以上代码的HTML渲染输出:

<td class=" text-center">
    <input class="bootstrapSwitch" data-on-color="success" data-on-text="Enable" data-off-text="Disable" data-size="mini" type="checkbox" checked="">
</td>

在UI中的输出:

tableSwitchableColumn 基函数:

// Function that render a Switchable column e.g Yes/No
cx.common.admin.tableSwitchableColumn = function(columnName, options){
    // Extend default options with specified options (if any)
    options = $.extend({
        editable: false,
        createdCell: null,
        dateEvaluatedAsYes: function(data){
            return data === '1' || data == 1;
        },
        onText: 'Yes',
        offText: 'No'
    }, cx.common.getValueOrDefault(options, {}));
    // Build the column settings object
    var columnSettings = {
        data: columnName,
        width: '25px',
        render: function(data){
            var checkedAttribute = options.dateEvaluatedAsYes(data) ? ' checked' : '';
            var disabledAttribute = options.editable ? '' : ' disabled';
            return '<input class="bootstrapSwitch" data-on-color="success" data-on-text="' + options.onText + '" data-off-text="'+ options.offText + '" data-size="mini" type="checkbox"' + checkedAttribute + disabledAttribute + '>';
        },
        className: 'text-center'
    };
    // If specified in the options, set the celle created callback
    if(options.createdCell !== null)
        columnSettings.createdCell = options.createdCell;
    // Return the column settings
    return columnSettings;
}

使用的版本:

Bootstrap v3.4.1, Bootstrap 切换 v3.3.4

你应该wrap it inside div tag with bootstrap appropriate classes.

<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>

<div class="custom-control custom-switch">
  <input type="checkbox" class="custom-control-input" id="customSwitch1">
  <label class="custom-control-label" for="customSwitch1">Toggle this switch element</label>
</div>

我在我的页面中添加了以下代码,它开始工作了

setInterval(function(){$('.bootstrapSwitch').bootstrapSwitch();}, 2000);
<head>
  <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
</head>

<body>
  <div class="custom-control custom-switch">
    <input type="checkbox" class="custom-control-input" id="customSwitch1">
    <label class="custom-control-label" for="customSwitch1">Toggle this switch</label>
  </div>
</body>

我遇到的问题是安装了 Bootstrap 4.1,但我需要 4.2。升级解决了这个问题。