0

Estou fazendo uma checkbox personalizada e quando clica, ela desaparece. Quero que, ao clicar nela, apareça True ou False sem que ela desapareça.

Quando eu retiro o evento onClick() a checkBox não desaparece, mas também não aparece True ou False.

Aqui está o código:

<style>
#customers {
  font-family: "Trebuchet MS", Arial, Helvetica, sans-serif;
  border-collapse: collapse;  
  margin-left: 5px;
  margin-top: 5px;
}

#customers td, #customers th {
  border: 1px solid #ddd;
  padding: 8px;
}

#customers tr:nth-child(even){background-color: #f2f2f2;}

#customers tr:hover {background-color: #ddd;}

#customers th {
  padding-top: 12px;
  padding-bottom: 12px;
  text-align: left;
  background-color: #4CAF50;
  color: white;
}


/* The container */
.container {
  display: block;
  position: relative;
  padding-left: 35px;
  margin-bottom: 4px;
  cursor: pointer;
   font-family: "Trebuchet MS", Arial, Helvetica, sans-serif;
  -webkit-user-select: none;
  -moz-user-select: none;
  -ms-user-select: none;
  user-select: none;
}

/* Hide the browser's default checkbox */
.container input {
  opacity: 0;
  cursor: pointer;
  height: 0;
  width: 0;
}

/* Create a custom checkbox */
.checkmark {
  position: absolute;
  top: 0;
  left: 0;
  height: 25px;
  width: 25px;
  background-color: #eee;
}

/* On mouse-over, add a grey background color */
.container:hover input ~ .checkmark {
  background-color: #ccc;
}

/* When the checkbox is checked, add a blue background */
.container input:checked ~ .checkmark {
  background-color: #2196F3;
}

/* Create the checkmark/indicator (hidden when not checked) */
.checkmark:after {
  content: "";
  position: absolute;
  display: none;
}

/* Show the checkmark when checked */
.container input:checked ~ .checkmark:after {
  display: block;
}

/* Style the checkmark/indicator */
.container .checkmark:after {
  left: 9px;
  top: 5px;
  width: 5px;
  height: 10px;
  border: solid white;
  border-width: 0 3px 3px 0;
  -webkit-transform: rotate(45deg);
  -ms-transform: rotate(45deg);
  transform: rotate(45deg);
  }
 
 
 
 
 
 input[type=text], select {
  width: 100%;
  padding: 12px 20px;
  margin: 8px 0;
  display: inline-block;
  border: 1px solid #ccc;
  border-radius: 4px;
  box-sizing: border-box;
}

input[type=submit] {
  width: 100%;
  background-color: #4CAF50;
  color: white;
  padding: 14px 20px;
  margin: 8px 0;
  border: none;
  border-radius: 4px;
  cursor: pointer;
}

input[type=submit]:hover {
  background-color: #45a049;
}

table{
			top: inherit;
             width: 310px;
             height: 350px;            
             overflow: auto;
             display: inline-block;
             margin-left: 5px;
             margin-top: 5px;
}
</style>
<body>

<table id="customers">
  <tr>
    <th colspan="2">Tabela</th>
  </tr>
  <tr>
    <td>Boolean</td>
    <td>
	<label class="container" id="lblBool">True</label>
	  <input type="checkbox" checked="checked" id="check" onclick="myCheck()"></input>
	  <span class="checkmark"></span>
</td>
</tr>
<script>
function myCheck() {
  var checkBox = document.getElementById("check");
  if (checkBox.checked == true){
  document.getElementById("lblBool").innerHTML = "False";
  } else {
  document.getElementById("lblBool").innerHTML = "True";
  }
}
</script>
</table>

</body>

2
  • 1
    Ta muito bagunçado o Html você abre uma tag label e fecha ela com uma tag input, tem td e tr sem fechamentos, etc..
    – LeAndrade
    Commented 26/06/2019 às 20:31
  • É verdade, corrigi esses detalhes, mas ainda permanece o erro.
    – sYsTeM
    Commented 26/06/2019 às 20:55

1 Resposta 1

3

Fiz alguns ajustes e parece estar funcionando do jeito que deveria.

  • O HTML estava quebrado (por exemplo, </input> aonde deveria ser </label>, <tr> sem fechar, etc).

  • A função myCheck() estava trocando o texto mas não o estado da checkbox.

function myCheck() {
  var checkBox = document.getElementById("check");
  if (checkBox.checked == true){
    document.getElementById("check").checked = false;
    document.getElementById("lblBool").innerHTML = "False";
  } else {
    document.getElementById("check").checked = true;
    document.getElementById("lblBool").innerHTML = "True";
  }
}
#customers {
  font-family: "Trebuchet MS", Arial, Helvetica, sans-serif;
  border-collapse: collapse;  
  margin-left: 5px;
  margin-top: 5px;
}

#customers td, #customers th {
  border: 1px solid #ddd;
  padding: 8px;
}

#customers tr:nth-child(even){background-color: #f2f2f2;}

#customers tr:hover {background-color: #ddd;}

#customers th {
  padding-top: 12px;
  padding-bottom: 12px;
  text-align: left;
  background-color: #4CAF50;
  color: white;
}


/* The container */
.container {
  display: block;
  position: relative;
  padding-left: 35px;
  margin-bottom: 4px;
  cursor: pointer;
   font-family: "Trebuchet MS", Arial, Helvetica, sans-serif;
  -webkit-user-select: none;
  -moz-user-select: none;
  -ms-user-select: none;
  user-select: none;
}

/* Hide the browser's default checkbox */
.container input {
  opacity: 0;
  cursor: pointer;
  height: 0;
  width: 0;
}

/* Create a custom checkbox */
.checkmark {
  position: absolute;
  top: 0;
  left: 0;
  height: 25px;
  width: 25px;
  background-color: #eee;
}

/* On mouse-over, add a grey background color */
.container:hover input ~ .checkmark {
  background-color: #ccc;
}

/* When the checkbox is checked, add a blue background */
.container input:checked ~ .checkmark {
  background-color: #2196F3;
}

/* Create the checkmark/indicator (hidden when not checked) */
.checkmark:after {
  content: "";
  position: absolute;
  display: none;
}

/* Show the checkmark when checked */
.container input:checked ~ .checkmark:after {
  display: block;
}

/* Style the checkmark/indicator */
.container .checkmark:after {
  left: 9px;
  top: 5px;
  width: 5px;
  height: 10px;
  border: solid white;
  border-width: 0 3px 3px 0;
  -webkit-transform: rotate(45deg);
  -ms-transform: rotate(45deg);
  transform: rotate(45deg);
  }
 
 
 
 
 
 input[type=text], select {
  width: 100%;
  padding: 12px 20px;
  margin: 8px 0;
  display: inline-block;
  border: 1px solid #ccc;
  border-radius: 4px;
  box-sizing: border-box;
}

input[type=submit] {
  width: 100%;
  background-color: #4CAF50;
  color: white;
  padding: 14px 20px;
  margin: 8px 0;
  border: none;
  border-radius: 4px;
  cursor: pointer;
}

input[type=submit]:hover {
  background-color: #45a049;
}

table{
			top: inherit;
             width: 310px;
             height: 350px;            
             overflow: auto;
             display: inline-block;
             margin-left: 5px;
             margin-top: 5px;
}
<table id="customers">
  <tr>
    <th colspan="2">Tabela</th>
  </tr>
  <tr>
    <td>Boolean</td>
    <td>
      <div class="container">
        <label id="lblBool">True</label>
        <input type="checkbox" checked="checked" id="check"  />
        <span class="checkmark" onclick="myCheck();"></span>
      </div>
    </td>
  </tr>
</table>

Você deve fazer log-in para responder a esta pergunta.

Esta não é a resposta que você está procurando? Pesquise outras perguntas com a tag .