Eu tenho um formulário dinâmico que possui várias caixas de seleção. Eu quero criar um método "selecionar tudo" que, quando clicado, verifica automaticamente todas as caixas no formulário.
[x] select all //<input type="checkbox" id="select-all"> Select all
[] item 1 //<input type="checkbox" id="1" class="checkbox1" value="1">
[] item 2 //<input type="checkbox" id="2" class="checkbox1" value="2">
[submit]
Meu jQuery é o seguinte:
$(document).ready(function(){
$("#select-all").click(function() {
if (this.checked)
{
console.log("Check detected");
$(".checkbox1").each(function() {
console.log("Checking the item with value:" + this.value );
$(this).prop("checked", true);
console.log(this);
});
} else {
console.log("not checked");
}
});
});
Saída do meu console:
> Check detected
> Checking the item with value:1
> <input type="checkbox" id="1" class="checkbox1" value="1">
> Checking the item with value:2
> <input type="checkbox" id="2" class="checkbox1" value="2">
Sou capaz de percorrer cada item, no entanto, não sei como definir a caixa de seleção como marcada.
A parte com a qual estou lutando é a configuração real do estado verificado, sei que preciso usar: .prop ("marcado", true), no entanto, o que devo usar para o elemento real? $ (isto) obviamente não funciona ...
$(this).prop("checked", true);
Respostas:
0 para resposta № 1Use este código simples
$(".checkAll").change(function(){
$(".checkbox1").prop("checked", this.checked);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<label>Check all</label>
<input type="checkbox" class="checkAll" />
<br/><br/>
<input type="checkbox" class="checkbox1" />
<input type="checkbox" class="checkbox1" />
<input type="checkbox" class="checkbox1" />
<input type="checkbox" class="checkbox1" />
0 para resposta № 2
Você está se referindo #select-all
Como (this.checked)
, Você precisa se referir como $(this)
.
Em segundo lugar você pode usar :verificado para descobrir se está marcado ou não.
Em terceiro lugar, você não precisa fazer um loop $(".checkbox1")
. jquery irá corresponder a todos os elementos e atualizará o atributo.
Abaixo está o trecho que pode ser útil
$(document).ready(function() {
$("#select-all").click(function() {
if ($(this).is(":checked")) {
console.log("Check detected");
$(".checkbox1").prop("checked", true)
} else {
$(".checkbox1").prop("checked", false)
}
});
// If select-all is selected and if one check box is unchecked ,
//then select-all will be unchecked
$(".checkbox1").click(function(event) {
if ($(this).is(":checked")) {
// #select-all must be checked when all checkbox are checked
} else {
if ($("#select-all").is(":checked")) {
$("#select-all").prop("checked", false)
}
}
})
});
0 para resposta № 3
código js
$(".checkbox1").click(function(event) {
var _this = $(this);
if (!_this.prop("checked")) {
$("#select-all").prop("checked", false)
}
})
$("#select-all").click(function() {
var _this = $(this);
var _value = _this.prop("checked");
console.log("Check detected");
$(".checkbox1").each(function() {
console.log("Checking the item with value:" + this.value);
$(this).prop("checked", _value);
console.log(this);
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="checkbox" id="select-all"> Select all
<input type="checkbox" id="1" class="checkbox1" value="1">
<input type="checkbox" id="2" class="checkbox1" value="2">
0 para a resposta № 4
Apenas para responder a esta pergunta. Tentei as várias soluções oferecidas e elas pareciam não funcionar. No entanto, percebi que, devido ao uso do Jquery Uniform, era necessário adicionar um pouco de código adicional para que o monitor funcionasse corretamente:
Esta postagem foi útil.
Obrigado a todos que dedicaram um tempo para responder.
Meu código final:
$(document).ready(function(){
$("#select-all").click(function() {
if (this.checked)
{
$(".checkbox1").prop("checked", true);
} else {
$(".checkbox1").prop("checked", false);
}
$.uniform.update(".checkbox1");
});
});