【问题标题】:checkbox default checked="checked" is not working?复选框默认选中=“已检查”不起作用?
【发布时间】:2019-05-06 10:42:37
【问题描述】:

复选框默认选中无效! 我试图修复它,但我找不到这里的错误? 所以在页面加载被选中,在页面加载后未被选中!? 我试过了

<div class="onoffswitch" style="margin: 0 auto;">
<input type="checkbox" class="avacheckbox onoffswitch-checkbox" id="AvButtonAutoGames" checked="checked"/>
<label class="onoffswitch-label" for="AvButtonAutoGames">
    <span class="onoffswitch-inner"></span>
    <span class="onoffswitch-switch"></span>
</label>
</div>

还有那个

  <div class="onoffswitch" style="margin: 0 auto;">
    <input type="checkbox" class="avacheckbox onoffswitch-checkbox" id="AvButtonAutoGames" checked/>
    <label class="onoffswitch-label" for="AvButtonAutoGames">
        <span class="onoffswitch-inner"></span>
        <span class="onoffswitch-switch"></span>
    </label>
    </div>

【问题讨论】:

  • 实际上,您的代码适用于我的&lt;input checked /&gt; 并检查MDN,那么您到底需要什么?

标签: javascript jquery html checkbox


【解决方案1】:

要指定值,使用checked="true",否则不要指定属性。

Checked: <input type="checkbox" checked="true" /><br/>
Not checked: <input type="checkbox" />

【讨论】:

    【解决方案2】:

    这很难看,但你是在使用这种语法来定义复选框的值吗?

    document.getElementById('AvButtonAutoGames').checked = true
    

    如果是这样,这是不正确的 - 但在第一个 JS 示例中,您有一个正确的 prop 用法:

    $('#AvButtonAutoGames').prop('checked', true)
    

    HTML5 一样,要应用的语法只是&lt;input checked&gt; 或为了清晰起见扩展&lt;input checked="checked"&gt;。缺少checked 属性会导致未选中的输入字段。

    只需将您的 .checked=X 转换为函数调用,它应该可以工作

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-02-08
      • 2016-03-13
      • 2023-03-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多