如何更改输入检查属性

how to change a input check attribute

当我单击 dropdown 中的 username1 时,右侧卡片中的 sepc view 切换将是 off.But 代码无效。 html代码:

<div class="container">
  <div class="row row-cols-2">
    <div class="col">
      <div class="card">
        <div class="card-body">
          <div class="dropdown">

            <button class="btn btn-secondary dropdown-toggle" type="button" id="dropdownMenuButton" data-toggle="dropdown"  aria-haspopup="true" aria-expanded="false">
              Dropdown button
            </button>
            
            <div class="dropdown-menu" aria-labelledby="dropdownMenuButton">
              <a class="dropdown-item" href="#" id="username1">Username1</a>
              <a class="dropdown-item" href="#" id="username2">Username2</a>
              <a class="dropdown-item" href="#" id="username3">Username3</a>
            </div>

          </div>
        </div>
      </div>

    </div>
    <div class="col">
      <div class="card">
        <div class="card-body">
          <div id="switch">

            <label for="fname">Spec View</label>&nbsp&nbsp<input id="specview" type="checkbox" checked data-toggle="toggle" data-onstyle="info" data-offstyle="light"></br>

            <label for="fname">Spec Edit</label>&nbsp&nbsp<input type="checkbox" checked data-toggle="toggle" data-onstyle="info" data-offstyle="light"></br>
            <label for="fname">Attribute Edit</label>&nbsp&nbsp<input type="checkbox" checked data-toggle="toggle" data-onstyle="info" data-offstyle="light"></br>
            <label for="fname">Operation Edit</label>&nbsp&nbsp<input type="checkbox" checked data-toggle="toggle" data-onstyle="info" data-offstyle="light"></br>
            <label for="fname">Section Design</label>&nbsp&nbsp<input type="checkbox" checked data-toggle="toggle" data-onstyle="info" data-offstyle="light"></br>
            <label for="fname">Cat-Template</label>&nbsp&nbsp<input type="checkbox" checked data-toggle="toggle" data-onstyle="info" data-offstyle="light"></br>
            <label for="fname">Template Design</label>&nbsp&nbsp<input type="checkbox" checked data-toggle="toggle" data-onstyle="info" data-offstyle="light"></br>
            <label for="fname">Enumeration</label>&nbsp&nbsp<input type="checkbox" checked data-toggle="toggle" data-onstyle="info" data-offstyle="light"></br>
            <label for="fname">User Role</label>&nbsp&nbsp<input type="checkbox" checked data-toggle="toggle" data-onstyle="info" data-offstyle="light"></br>
            <label for="fname">Formula</label>&nbsp&nbsp<input type="checkbox" checked data-toggle="toggle" data-onstyle="info" data-offstyle="light"></br>
            <label for="fname">Data Mapping</label>&nbsp&nbsp<input type="checkbox" checked data-toggle="toggle" data-onstyle="info" data-offstyle="light"></br>
            <label for="fname">Operating Log</label>&nbsp&nbsp<input type="checkbox" checked data-toggle="toggle" data-onstyle="info" data-offstyle="light"></br>
          </div>
        </div>
      </div>
    </div>

  </div>
</div>

js代码:

$(document).ready(function(){
  $("#username1").click(function(){
  var elmnt = document.getElementById("specview");
  var attr = elmnt.getAttributeNode("checked");
  elmnt.removeAttributeNode(attr);
     
  });
});

Bootstrap 切换我使用:https://www.bootstraptoggle.com/ 我的项目 link:https://codepen.io/nutkin/pen/KKgJNMd

请按照以下代码示例进行操作:

检查

document.getElementById("checkbox").checked = true;

取消勾选

document.getElementById("checkbox").checked = false;

根据documentation

$('#toggle-demo').bootstrapToggle('on') // Sets the toggle to 'On' state

因此,您需要将单击事件处理程序更改为:

$("#username1").on('click', function(e) {
    $('#specview').bootstrapToggle('on');
});

片段:

$("#username1").on('click', function(e) {
    $('#specview').bootstrapToggle('on');
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.5.3/css/bootstrap.min.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.5.3/js/bootstrap.bundle.min.js"></script>
<link href="https://gitcdn.github.io/bootstrap-toggle/2.2.2/css/bootstrap-toggle.min.css" rel="stylesheet">
<script src="https://gitcdn.github.io/bootstrap-toggle/2.2.2/js/bootstrap-toggle.min.js"></script>

<div class="container">
  <div class="row row-cols-2">
    <div class="col">
      <div class="card">
        <div class="card-body">
          <div class="dropdown">

            <button class="btn btn-secondary dropdown-toggle" type="button" id="dropdownMenuButton" data-toggle="dropdown"  aria-haspopup="true" aria-expanded="false">
              Dropdown button
            </button>
            
            <div class="dropdown-menu" aria-labelledby="dropdownMenuButton">
              <a class="dropdown-item" href="#" id="username1">Username1</a>
              <a class="dropdown-item" href="#" id="username2">Username2</a>
              <a class="dropdown-item" href="#" id="username3">Username3</a>
            </div>

          </div>
        </div>
      </div>

    </div>
    <div class="col">
      <div class="card">
        <div class="card-body">
          <div id="switch">

            <label for="fname">Spec View</label>&nbsp&nbsp<input id="specview" type="checkbox" checked data-toggle="toggle" data-onstyle="info" data-offstyle="light"></br>

            <label for="fname">Spec Edit</label>&nbsp&nbsp<input type="checkbox" checked data-toggle="toggle" data-onstyle="info" data-offstyle="light"></br>
            <label for="fname">Attribute Edit</label>&nbsp&nbsp<input type="checkbox" checked data-toggle="toggle" data-onstyle="info" data-offstyle="light"></br>
            <label for="fname">Operation Edit</label>&nbsp&nbsp<input type="checkbox" checked data-toggle="toggle" data-onstyle="info" data-offstyle="light"></br>
            <label for="fname">Section Design</label>&nbsp&nbsp<input type="checkbox" checked data-toggle="toggle" data-onstyle="info" data-offstyle="light"></br>
            <label for="fname">Cat-Template</label>&nbsp&nbsp<input type="checkbox" checked data-toggle="toggle" data-onstyle="info" data-offstyle="light"></br>
            <label for="fname">Template Design</label>&nbsp&nbsp<input type="checkbox" checked data-toggle="toggle" data-onstyle="info" data-offstyle="light"></br>
            <label for="fname">Enumeration</label>&nbsp&nbsp<input type="checkbox" checked data-toggle="toggle" data-onstyle="info" data-offstyle="light"></br>
            <label for="fname">User Role</label>&nbsp&nbsp<input type="checkbox" checked data-toggle="toggle" data-onstyle="info" data-offstyle="light"></br>
            <label for="fname">Formula</label>&nbsp&nbsp<input type="checkbox" checked data-toggle="toggle" data-onstyle="info" data-offstyle="light"></br>
            <label for="fname">Data Mapping</label>&nbsp&nbsp<input type="checkbox" checked data-toggle="toggle" data-onstyle="info" data-offstyle="light"></br>
            <label for="fname">Operating Log</label>&nbsp&nbsp<input type="checkbox" checked data-toggle="toggle" data-onstyle="info" data-offstyle="light"></br>
          </div>
        </div>
      </div>
    </div>
  </div>
</div>

您在 jquery 中混合了 javascript。实际上你需要使用 toggle class 来打开开关 示例项目在这里 https://codepen.io/exclutips/pen/qBavRWM

$(document).ready(function(){
  $("#username1").click(function(){
    $('#specview').parent('.toggle').toggleClass('on off');
    $('#specview').removeAttr('checked');
  });
});

$(document).ready(function(){
  $("#username1").click(function(){
    $('#specview').parent('.toggle').toggleClass('on off');
    $('#specview').removeAttr('checked');
  });
});
<link href="https://cdn.jsdelivr.net/npm/bootstrap@4.5.3/dist/css/bootstrap.min.css" rel="stylesheet"/>
<link href="https://cdn.jsdelivr.net/gh/gitbrent/bootstrap4-toggle@3.6.1/css/bootstrap4-toggle.min.css" rel="stylesheet"/>

<script src="https://code.jquery.com/jquery-3.5.1.slim.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.1/dist/umd/popper.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@4.5.3/dist/js/bootstrap.min.js"></script>
<script src="https://gitcdn.github.io/bootstrap-toggle/2.2.2/js/bootstrap-toggle.min.js"></script>

<script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.1/dist/umd/popper.min.js"></script>

<div class="container">
  <div class="row row-cols-2">
    <div class="col">
      <div class="card">
        <div class="card-body">
          <div class="dropdown">

            <button class="btn btn-secondary dropdown-toggle" type="button" id="dropdownMenuButton" data-toggle="dropdown"  aria-haspopup="true" aria-expanded="false">
              Dropdown button
            </button>
            
            <div class="dropdown-menu" aria-labelledby="dropdownMenuButton">
              <a class="dropdown-item" href="#" id="username1">Username1</a>
              <a class="dropdown-item" href="#" id="username2">Username2</a>
              <a class="dropdown-item" href="#" id="username3">Username3</a>
            </div>

          </div>
        </div>
      </div>

    </div>
    <div class="col">
      <div class="card">
        <div class="card-body">
          <div id="switch">

            <label for="fname">Spec View</label>&nbsp&nbsp<input id="specview" type="checkbox" checked data-toggle="toggle" data-onstyle="info" data-offstyle="light"></br>

            <label for="fname">Spec Edit</label>&nbsp&nbsp<input type="checkbox" checked data-toggle="toggle" data-onstyle="info" data-offstyle="light"></br>
            <label for="fname">Attribute Edit</label>&nbsp&nbsp<input type="checkbox" checked data-toggle="toggle" data-onstyle="info" data-offstyle="light"></br>
            <label for="fname">Operation Edit</label>&nbsp&nbsp<input type="checkbox" checked data-toggle="toggle" data-onstyle="info" data-offstyle="light"></br>
            <label for="fname">Section Design</label>&nbsp&nbsp<input type="checkbox" checked data-toggle="toggle" data-onstyle="info" data-offstyle="light"></br>
            <label for="fname">Cat-Template</label>&nbsp&nbsp<input type="checkbox" checked data-toggle="toggle" data-onstyle="info" data-offstyle="light"></br>
            <label for="fname">Template Design</label>&nbsp&nbsp<input type="checkbox" checked data-toggle="toggle" data-onstyle="info" data-offstyle="light"></br>
            <label for="fname">Enumeration</label>&nbsp&nbsp<input type="checkbox" checked data-toggle="toggle" data-onstyle="info" data-offstyle="light"></br>
            <label for="fname">User Role</label>&nbsp&nbsp<input type="checkbox" checked data-toggle="toggle" data-onstyle="info" data-offstyle="light"></br>
            <label for="fname">Formula</label>&nbsp&nbsp<input type="checkbox" checked data-toggle="toggle" data-onstyle="info" data-offstyle="light"></br>
            <label for="fname">Data Mapping</label>&nbsp&nbsp<input type="checkbox" checked data-toggle="toggle" data-onstyle="info" data-offstyle="light"></br>
            <label for="fname">Operating Log</label>&nbsp&nbsp<input type="checkbox" checked data-toggle="toggle" data-onstyle="info" data-offstyle="light"></br>
          </div>
        </div>
      </div>
    </div>

  </div>
</div>