How to get the value of the value and also the name of the checkbox at the same time as selecting jquery

3

I need to get the name of the checked checkbox as soon as I click on one of the checkbox on the page. %% I already have, but I also need to get the value of value .

Someone could help me or give me another alternative as I need to get the value of name and name at the same time as clicking.

$(document).ready(function () {
		
	$('input[name="1117"]').click(function () { 
		selecionado('1117'); 
	}); 
	var selecionado = function (grupo) { 
		var result = $('input[name="' + grupo + '"]:checked'); 
		if (result.length > 0) { 
			var contador = result.length + " selecionado(s)<br/>"; 
			result.each(function () { 
				contador += $(this).val() + " "
			}); 
			$('#divFiltros').html(contador); 
		} 
		else { 
			$('#divFiltros').html("Nenhum selecionado"); 
		} 
	}; 
}); 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script><divid="divFiltros"></div>

<form method="POST"> 
      
      <legend>Marca</legend>
      
      <div class="checkbox checkbox-success">
           <input class="styled marca" type="checkbox" id="6327" name="1117" value="6327">
           <label for="">Samsung</label>
      </div>
      <div class="checkbox checkbox-success">
           <input class="styled marca" type="checkbox" id="6327" name="1117" value="6328">
           <label for="">Motorola</label>
      </div>
      <div class="checkbox checkbox-success">
           <input class="styled marca" type="checkbox" id="6327" name="1117" value="6329">
           <label for="">Sony</label>
      </div>
      <div class="checkbox checkbox-success">
           <input class="styled marca" type="checkbox" id="6327" name="1117" value="6330">
           <label for="">LG</label>
      </div>
      
      <legend>Especificação</legend>
      
      <div class="checkbox checkbox-success">
           <input class="styled marca" type="checkbox" id="450" name="1120" value="450">
           <label for="450">4G</label>
      </div>
      <div class="checkbox checkbox-success">
           <input class="styled marca" type="checkbox" id="455" name="1120" value="455">
           <label for="455">2 Chips</label>
      </div>
      <div class="checkbox checkbox-success">
           <input class="styled marca" type="checkbox" id="461" name="1120" value="461">
           <label for="461">Video 4K</label>
      </div>
</form>
    
asked by anonymous 12.02.2016 / 20:13

1 answer

1

The name is a property of the object. It is also an attribute of the element that this object represents.

That means you can fetch this value with .name or getAttribute('name'); .

So if what you are looking for is what items have been selected you can use:

var escolhidos = $('input:checked');

to find out what the checkbox is marked, and then map that array only with what matters: name and value .

var escolhidos = $('input:checked');
var selecionados = escolhidos.map(function() {
     var produto = {nome: this.name, value: this.value};
     return produto;
}).get(); // o .get() é para discartar o jquery e trabalhar só com uma array nativa

example: link

    
12.02.2016 / 20:39