Jquery search in input checked the value of a [duplicate] property

1

I have the following html code

<input codprueba="Prueba1" type="checkbox" title="Observaciones Prueba1">
<input codprueba="Prueba2" type="checkbox" title="Observaciones Prueba2">
<input codprueba="Prueba3" type="checkbox" title="Observaciones Prueba3">

And through JQuery, I want to concatenate in a variable separated by "|" , for example, the value they have in the attribute codprueba .

That is, if the first and third checkboxes are checked back var cadena = 'Prueba1|Prueba2'

What I have come to is the following code, but I do not know how to continue.

var cadena = $("input[type=checkbox]").prop("checked", true).attr("codprueba");

alert(cadena);

    var cadena = $("input[type=checkbox]").prop("checked", true).attr("codprueba");

    alert(cadena);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input codprueba="Prueba1" type="checkbox" title="Observaciones Prueba1">
    <input codprueba="Prueba2" type="checkbox" title="Observaciones Prueba2">
    <input codprueba="Prueba3" type="checkbox" title="Observaciones Prueba3">
    
asked by Luis 02.03.2017 в 10:02
source

3 answers

1

You have to go through all the input selected to check their values one by one and insert them in an array or go forming the string according to the format you need.

var selected = [];
$('input:checked').each(function() {
    selected.push($(this).attr('codprueba'));
});
alert(selected);

To avoid conflicts with other input, I recommend that you use the name property to uniquely name these inputs. In this way you could do:

$("input:checkbox[name=my_name]:checked").each(function(){
    selected.push($(this).attr('codprueba'));
});

You can also insert them within a div with a id and select the inputs within that div:

$('#my_div_id input:checked')...

To join the array, the following sentence would suffice:

var str = selected.join("|");
    
answered by 02.03.2017 / 10:27
source
1

To save the values of checkbox that are marked use the following (I have put the attribute name ):

var yourArray = new Array();

$("input:checkbox[name=checks]:checked").each(function(){
    yourArray.push($(this).attr('codprueba'));
});

Your checkbox will be declared as follows:

<input codprueba="Prueba1" type="checkbox" title="Observaciones Prueba1" name="checks" checked>
<input codprueba="Prueba2" type="checkbox" title="Observaciones Prueba2" name="checks" checked>
<input codprueba="Prueba3" type="checkbox" title="Observaciones Prueba3" name="checks" >

The checked attribute I have set to debug

To write the result exactly how you want it, it would be:

 document.write(yourArray.join("|"));   
    
answered by 02.03.2017 в 10:22
0

To ensure that the input contains the attribute "codetest" use the selector "input [checkmark]: checked" to scroll through all the input. Here is the code example:

var arr = [];
$.map($("input[codprueba]:checked"), function(elem) {
    arr.push($(elem).attr("codprueba"));
}
var result = arr.join("|");
    
answered by 02.03.2017 в 11:26