【问题标题】:input type checkbox to be true or false depended on the data输入类型复选框为真或假取决于数据
【发布时间】:2019-03-26 05:57:35
【问题描述】:

我正在尝试创建一个复选框,它依赖于数据库来显示它最初应该被选中还是取消选中。

使用真/假、0/1 和 ""/blanks 围绕复选框和值进行播放

$.each(customers, (index, customer) => {
$('#accordionAlarm').append(
`<tr>
<td class="card">

<input type="checkbox" name="checkBox[]" id="1${customer.customerId}" checked="${customer.alarmDateInitial === 'Y' ?  'true' : 'false' }"></input>
<input type="checkbox" name="checkBox[]" id="5${customer.customerId}" checked="${customer.alarmDateFifth === 'Y' ?  'true' : 'false' }"></input>
<input type="checkbox" name="checkBox[]" id="7${customer.customerId}" checked="${customer.alarmDateSeventh === 'Y' ?  'true' : 'false' }"></input>

</td>
</tr>`)
});

预期:

  • 当数据 = Y 时,复选框被选中
  • 当 data != Y 时,复选框未被选中

【问题讨论】:

    标签: javascript html checkbox


    【解决方案1】:

    checkedboolean attribute,这意味着它的存在表明该元素已被检查。

    您必须重组模板文字,以便仅在条件为 true 时才包含 checked,例如

    customer.alarmDateInitial === 'Y' ?  'checked' : ''
    

    在上下文中:

    `<input type="checkbox" name="checkBox[]" id="1${customer.customerId}" ${customer.alarmDateInitial === 'Y' ?  'checked' : '' } ></input>`
    

    【讨论】:

      【解决方案2】:

      您是否尝试过checked="checked" 而不是真实值?您也可以只返回 checked 而不是设置属性值。

      【讨论】:

        猜你喜欢
        • 2014-05-10
        • 2022-07-05
        • 2015-06-03
        • 1970-01-01
        • 1970-01-01
        • 2016-09-01
        • 2017-10-14
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多