J'ai plusieurs cases à cocher avec name=ap[]
et je veux utiliser jquery pour convertir les valeurs de case à cocher cochées en valeurs séparées par des virgules afin que je puisse analyser comme une chaîne de requête. Comment j'irais à propos de ça? .serialize()
rend la chaîne plus compliquéeFormat checkbox array to csv
1
A
Répondre
6
jQuery.fn.map itterates sur un ensemble jQuery, et retourne un nouveau tableau (à l'intérieur d'une enveloppe jQuery) qui contient des noeuds égal à la valeur de retour de chaque itterarion. Vous pouvez alors obtenir ce tableau en appelant jQuery.fn.get, utilisez ce tableau pour tout ce que vous voulez:
$(':checkbox:checked').map(function(){
return $(this).val(); // value will be in the returned array
})
.get() // extract the new array from the jQuery object
.join(','); //do anything you want with the array
Dans votre cas, cela peut être appliqué en tant que tel:
var list = $(':checkbox[name="ap[]"]:checked').map(function(){
return $(this).val();
}).get().join(',');
alert(list);
1
var list = '';
$("[name=ap[]]").is("checked").each(function(){
list += $(this).val() + ',';
});
+0
+1 pour la simplicité, mais a besoin d'un imo de clôture-poste, cela va laisser une virgule traînante –
0
var csv="";
$('input:checkbox[name="ap[]"]:checked').each(function(index) {
if(csv !="") csv+=",";
csv+=$(this).val();
});
Questions connexes
- 1. Python: nd array to csv
- 2. PHP CSV string to array
- 3. csv to array overide data
- 4. CSV to array - Problème jquery et ajax
- 5. PHP Checkbox array validant
- 6. Checkbox into Array traitement
- 7. Checkbox array dans ruby
- 8. Checkbox Array, Jquery, PHP
- 9. CheckBox Array dans VB.net
- 10. Checkbox Post Array vide
- 11. PHP CHECKBOX Array Numéro
- 12. Array Array to Object Array
- 13. String array to Int array
- 14. Add Array To Array
- 15. Java Array to ArrayList to Array
- 16. Explode to Array
- 17. Exportingm DataTable to csv
- 18. checkbox label to string
- 19. C# CheckBox to Button
- 20. checkbox to string conversion
- 21. Checkbox to textview android?
- 22. char array to int array
- 23. Byte Array to Double Array
- 24. JRuby Array to Java Array
- 25. Fetch Array - Combiner certains résultats au format CSV
- 26. checkbox array loop in C#
- 27. CSV date Format format
- 28. Javascript Array Integer Array Format
- 29. ActionScript/Flex string to array
- 30. Rechercher array de CSV
merci @avaq j'ai regardé .map mais n'a pas tout à fait le comprendre pleinement. Alors, comment pourrais-je accéder/alerter le csv? 'alert ($ (: case à cocher: cochée) .get());' ?? – JPickup
Je vais modifier ma réponse pour répondre à votre question plus précisément, un moment. – Avaq
est-ce aussi plus efficace que d'utiliser .each ou simplement préféré car c'est moins de code? – JPickup