Communiquez avec les autres et partagez vos connaissances professionnelles

Inscrivez-vous ou connectez-vous pour rejoindre votre communauté professionnelle.

Suivre

How can I get a checkbox's value in jQuery?

user-image
Question ajoutée par Zaid Rabab'a , Software Development Team Leader , Al-Safa Co. Ltd.
Date de publication: 2013/04/08
Hazem Salama
par Hazem Salama , Senior Member of Technical Staff , Verizon Communications

If you mean the value that you have in the value attribute of the checkbox then you can use this //Assuming your check box id is chkBox $('#chkBox').val(); If you mean the state, whether checked or not, you can use plain old javascript document.getElementById('chkBox').checked; // will return false or true depending on the state // OR in jquery $('#chkBox').is(':checked');

Muhammad Majid Saleem
par Muhammad Majid Saleem , Senior PHP Developer / Project Manager , SwaamTech

There are two parts of the question I guess.
They are: 1.
You want to get checkbox value regardless of whether it is checked or not? 2.
You want to get THE "Checked" checkbox value.
Answers: 1.
You can get checkbox value: $("#checkbox_id").val(); $(".checkbox_class").val(); // this is return in array if particular class is applied on many other fields or checkbox.
2.
You can get Checed checkbox value: if( $("#checkbox_id").attr("checked") ) { $("#checkbox_id").val(); }

Faizan Ahmad
par Faizan Ahmad , Software Engineer , cardekho.com

on check box change event $('#checkboxid').change(function(){ if($('#checkboxid').is('checked')) { var val = $('#checkboxid').val(); console.log(val); } });

Utilisateur supprimé
par Utilisateur supprimé

function checkboxValues() { var values = []; $('#checkbox :checked').each(function() { values.push($(this).val()); }); console.log(values); } don't forget to enable firebug to see the results .

Panneer selvam xavier
par Panneer selvam xavier , Supply Chain Analyst / Trainer , Infonet

$("input[type='checkbox']").val(); Or if you have set a class or id for it, you can: $('#check_id').val(); $('.check_class').val(); Also you can check whether it is checked or not like: if ($('#check_id').is(":checked")) { // it is checked }

Mohamed Sadek Kamal Mohamed
par Mohamed Sadek Kamal Mohamed , Front-End Engineer , Reuters Dubai

.is(':checked')

Utilisateur supprimé
par Utilisateur supprimé

$("#theChk").prop("checked") 

$("#theChk").attr("checked") 

$("#theChk").val()

Besim Dauti
par Besim Dauti , Ceo / Front End Developer , Nunforest

if ($('#check_id').is(":checked")) { // it is checked }

More Questions Like This

Avez-vous besoin d'aide pour créer un CV ayant les mots-clés recherchés par les employeurs?