logo

JavaScript'te tüm işaretli onay kutusu değerleri nasıl alınır?

Onay kutusu, kullanıcıların işaretleyerek ve işaretini kaldırarak ikili seçim (doğru veya yanlış) yapmasına olanak tanıyan bir seçim kutusudur. Temel olarak onay kutusu, kullanıcıdan bir veya daha fazla girdi almak için GUI formlarında ve uygulamalarında sıklıkla kullanılan bir simgedir.

  • Bir onay kutusu işaretlenirse veya işaretlenirse, şunu belirtir: doğru ; bu, kullanıcının değeri seçtiği anlamına gelir.
  • Bir onay kutusunun işareti kaldırılmışsa veya işaretlenmemişse, şunu belirtir: YANLIŞ ; bu, kullanıcının değeri seçmediği anlamına gelir.

Bunu hatırla onay kutusu, aynı anda birden fazla seçime izin verdiği için radyo düğmesinden ve açılır listeden farklıdır. Bunun aksine, radyo düğmesi ve açılır menü, verilen seçeneklerden yalnızca birini seçmemize olanak tanır.

Bu bölümde, şimdi tüm işaretli onay kutusu değerlerinin nasıl alınacağını göreceğiz. JavaScript .

Onay kutusu söz dizimi oluştur

Bir onay kutusu oluşturmak için HTML sekmesini kullanın ve aşağıda gösterildiği gibi sekmenin içine ='checkbox' yazın -

 Yes 

Onay kutusu nesnesini JavaScript aracılığıyla oluşturarak da bir onay kutusu oluşturabilirsiniz ancak bu yöntem biraz karmaşıktır. Her iki yaklaşımı da daha sonra tartışacağız.

Örnekler

Onay kutusu değeri oluştur ve al

Bu örnekte, kullanıcının aralarında yalnızca bir onay kutusunu işaretlemesi şartıyla iki onay kutusu oluşturacağız. Daha sonra işaretli onay kutusunun değerini getireceğiz. Aşağıdaki koda bakın:

c# dize içerir

Kodu Kopyala

 <h2>Create a checkbox and get its value</h2> <h3> Are you a web developer? </h3> Yes: No: <br> <br> Submit <br> <h4> <h4> function checkCheckbox() { var yes = document.getElementById(&apos;myCheck1&apos;); var no = document.getElementById(&apos;myCheck2&apos;); if (yes.checked == true &amp;&amp; no.checked == true){ return document.getElementById(&apos;error&apos;).innerHTML = &apos;Please mark only one checkbox either Yes or No&apos;; } else if (yes.checked == true){ var y = document.getElementById(&apos;myCheck1&apos;).value; return document.getElementById(&apos;result&apos;).innerHTML = y; } else if (no.checked == true){ var n = document.getElementById(&apos;myCheck2&apos;).value; return document.getElementById(&apos;result&apos;).innerHTML = n; } else { return document.getElementById(&apos;error&apos;).innerHTML = &apos;*Please mark any of checkbox&apos;; } } </h4></h4>
Şimdi Test Edin

Çıktı

işaretlerseniz Evet onay kutusunu seçin ve ardından Göndermek düğmesine bastığınızda aşağıda gösterildiği gibi bir mesaj görüntülenecektir:

uzunluk dizisi
JavaScript'te tüm işaretli onay kutusu değerleri nasıl alınır?

Eğer üzerine tıklarsanız Göndermek Onay kutularından herhangi birini işaretlemeden düğmesine basarsanız, bir hata mesajı görüntülenecektir.

JavaScript'te tüm işaretli onay kutusu değerleri nasıl alınır?

Benzer şekilde, çıktıyı diğer koşullar için de kontrol edebilirsiniz.

Tüm onay kutusu değerini al

Artık kullanıcı tarafından işaretlenen tüm onay kutusu değerlerinin nasıl alınacağını göreceksiniz. Aşağıdaki örneğe bakın.

Kodu Kopyala

 <h2>Create a checkbox and get its value</h2> <h4> Select the programming language, you know </h4> <tr> <td> Java: </td> <td> PHP: </td> </tr> <tr> <td> Angular: </td> <td> CSS: </td> </tr> <tr> <td> Python: </td> <td> Android: </td> Check all <br> <br> Submit <br> <h4></h4> function checkAll() { var inputs = document.querySelectorAll(&apos;.pl&apos;); for (var i = 0; i <inputs.length; i++) { inputs[i].checked="true;" } function getcheckboxvalue() var l1="document.getElementById(&apos;check1&apos;);" l2="document.getElementById(&apos;check2&apos;);" l3="document.getElementById(&apos;check3&apos;);" l4="document.getElementById(&apos;check4&apos;);" l5="document.getElementById(&apos;check5&apos;);" l6="document.getElementById(&apos;check6&apos;);" res=" " ; if (l1.checked="=" true){ pl1="document.getElementById(&apos;check1&apos;).value;" + ','; else (l2.checked="=" pl2="document.getElementById(&apos;check2&apos;).value;" (l3.checked="=" document.write(res); pl3="document.getElementById(&apos;check3&apos;).value;" (l4.checked="=" pl4="document.getElementById(&apos;check4&apos;).value;" (l5.checked="=" pl5="document.getElementById(&apos;check5&apos;).value;" (l6.checked="=" pl6="document.getElementById(&apos;check6&apos;).value;" pl6; return document.getelementbyid('result').innerhtml="You have not selected anything" ' languages'; < pre> <span> Test it Now </span> <p> <strong>Output</strong> </p> <p>By executing this code, we will get a response like the below screenshot having some programming languages where you can choose the language you know.</p> <img src="//techcodeview.com/img/javascript-tutorial/63/how-get-all-checked-checkbox-value-javascript-3.webp" alt="How to get all checked checkbox value in JavaScript"> <p>Here, you click on the <strong>Check all</strong> button, it will mark all the programming language checkboxes. After that, click on the <strong>Submit</strong> button to get the response.</p> <img src="//techcodeview.com/img/javascript-tutorial/63/how-get-all-checked-checkbox-value-javascript-4.webp" alt="How to get all checked checkbox value in JavaScript"> <p>Although you can select the language one by one by marking an individual checkbox and then click on the <strong>Submit</strong> button to get the result.</p> <img src="//techcodeview.com/img/javascript-tutorial/63/how-get-all-checked-checkbox-value-javascript-5.webp" alt="How to get all checked checkbox value in JavaScript"> <p> <strong>Output: When you have not selected anything</strong> </p> <img src="//techcodeview.com/img/javascript-tutorial/63/how-get-all-checked-checkbox-value-javascript-6.webp" alt="How to get all checked checkbox value in JavaScript"> <h2>Get all marked checkboxes value using querySelectorAll() method</h2> <p>There is one more method to get all selected values from the checkboxes marked by the user. You will now see how to get the value of all checkboxes using the querySelectorAll() method marked by the user. This will fetch the checkboxes values from the HTML form and display the result.</p> <h3>Get all checkbox value</h3> <p>Now, you will see how to get all checkbox values marked by the user. See the below example.</p> <p> <strong>Copy Code</strong> </p> <pre> <h2> Get all marked checkboxes value </h2> <h4> Select the programming language, you know </h4> <tr> <td> Java: </td> <td> PHP: </td> </tr> <tr> <td> Angular: </td> <td> CSS: </td> </tr> <tr> <td> Python: </td> <td> Android: </td> <br> <br> Submit <br> <h4></h4> document.getElementById(&apos;btn&apos;).onclick = function() { var markedCheckbox = document.getElementsByName(&apos;pl&apos;); for (var checkbox of markedCheckbox) { if (checkbox.checked) document.body.append(checkbox.value + &apos; &apos;); } } </tr></pre> <span> Test it Now </span> <p> <strong>Output</strong> </p> <p>Here, you can see that all marked checkboxes value has been returned.</p> <img src="//techcodeview.com/img/javascript-tutorial/63/how-get-all-checked-checkbox-value-javascript-7.webp" alt="How to get all checked checkbox value in JavaScript"> <h3>Different JavaScript codes to get marked checkboxes value</h3> <p>JavaScript Code to get all checked checkbox values</p> <pre> document.getElementById(&apos;btn&apos;).onclick = function() { var markedCheckbox = document.getElementsByName(&apos;pl&apos;); for (var checkbox of markedCheckbox) { if (checkbox.checked) document.body.append(checkbox.value + &apos; &apos;); } } </pre> <p>You can also use the below code to get all checked checkboxes values.</p> <pre> document.getElementById(&apos;btn&apos;).onclick = function() { var markedCheckbox = document.querySelectorAll(&apos;input[type=&apos;checkbox&apos;]:checked&apos;); for (var checkbox of markedCheckbox) { document.body.append(checkbox.value + &apos; &apos;); } } </pre> <p>So, checkbox elements allow to multi-select. This means that the users can select one or more options of their choice defined in the HTML form. Even you can select all the checkboxes. In the above example, you have already seen that.</p> <hr></inputs.length;></tr>
Şimdi Test Edin

Çıktı

Burada işaretli tüm onay kutularının değerinin döndürüldüğünü görebilirsiniz.

JavaScript'te tüm işaretli onay kutusu değerleri nasıl alınır?

İşaretli onay kutularının değerini almak için farklı JavaScript kodları

İşaretli tüm onay kutusu değerlerini almak için JavaScript Kodu

 document.getElementById(&apos;btn&apos;).onclick = function() { var markedCheckbox = document.getElementsByName(&apos;pl&apos;); for (var checkbox of markedCheckbox) { if (checkbox.checked) document.body.append(checkbox.value + &apos; &apos;); } } 

Tüm işaretli onay kutularının değerlerini almak için aşağıdaki kodu da kullanabilirsiniz.

ofset yüksekliği
 document.getElementById(&apos;btn&apos;).onclick = function() { var markedCheckbox = document.querySelectorAll(&apos;input[type=&apos;checkbox&apos;]:checked&apos;); for (var checkbox of markedCheckbox) { document.body.append(checkbox.value + &apos; &apos;); } } 

Böylece onay kutusu öğeleri çoklu seçime izin verir. Bu, kullanıcıların HTML formunda tanımlanan bir veya daha fazla seçeneği seçebilecekleri anlamına gelir. Hatta tüm onay kutularını seçebilirsiniz. Yukarıdaki örnekte bunu zaten gördünüz.