logo

Kaip gauti visą pažymėtą žymimojo laukelio reikšmę „JavaScript“?

Žymimasis laukelis yra pasirinkimo laukelis, leidžiantis vartotojams pasirinkti dvejetainį pasirinkimą (teisingą arba klaidingą), jį pažymint ir panaikinant žymėjimą. Iš esmės žymimasis laukelis yra piktograma, kuri dažnai naudojama GUI formose ir programoje, norint gauti vieną ar daugiau vartotojo įvesties.

  • Jei žymimasis laukelis yra pažymėtas arba pažymėtas, tai reiškia, kad tiesa ; tai reiškia, kad vartotojas pasirinko vertę.
  • Jei žymimasis laukelis nepažymėtas arba nepažymėtas, jis nurodomas klaidinga ; tai reiškia, kad vartotojas nepasirinko vertės.

Prisiminti, kad žymimasis laukelis skiriasi nuo radijo mygtuko ir išskleidžiamojo sąrašo, nes leidžia vienu metu pasirinkti kelis. Priešingai, radijo mygtukas ir išskleidžiamasis meniu leidžia pasirinkti tik vieną iš pateiktų parinkčių.

Šiame skyriuje dabar pamatysime, kaip gauti visos pažymėtos žymės langelio reikšmės naudojant JavaScript .

Sukurti žymės langelio sintaksę

Norėdami sukurti žymimąjį laukelį, naudokite HTML skirtuką ir tipas='checkbox' skirtuko viduje, kaip parodyta toliau -

 Yes 

Nors jūs taip pat galite sukurti žymimąjį laukelį sukurdami žymės langelio objektą naudodami JavaScript, tačiau šis metodas yra šiek tiek sudėtingas. Abu būdus aptarsime vėliau –

dvejetainių medžių rūšys

Pavyzdžiai

Sukurkite ir gaukite žymimojo laukelio vertę

Šiame pavyzdyje sukursime du žymimuosius laukelius, tačiau su sąlyga, kad vartotojas turės pažymėti tik vieną žymimąjį laukelį tarp jų. Tada gausime pažymėto žymės langelio reikšmę. Žiūrėkite žemiau esantį kodą:

Kopijuoti kodą

 <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>
Išbandykite dabar

Išvestis

Jei pažymėsite Taip žymimąjį laukelį, tada spustelėkite Pateikti mygtuką, bus rodomas pranešimas, kaip parodyta žemiau:

Kaip gauti visą pažymėtą žymimojo laukelio reikšmę „JavaScript“.

Jei paspausite ant Pateikti mygtuką nepažymėjus nė vieno iš žymimųjų laukelių, bus rodomas klaidos pranešimas.

Kaip gauti visą pažymėtą žymimojo laukelio reikšmę „JavaScript“.

Panašiai galite patikrinti išvestį dėl kitų sąlygų.

Gauti visą žymimąjį laukelį

Dabar pamatysite, kaip gauti visas vartotojo pažymėtas žymės langelio reikšmes. Žiūrėkite žemiau esantį pavyzdį.

Kopijuoti kodą

 <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>
Išbandykite dabar

Išvestis

Čia galite pamatyti, kad visų pažymėtų žymimųjų laukelių reikšmė buvo grąžinta.

Kaip gauti visą pažymėtą žymimojo laukelio reikšmę „JavaScript“.

Skirtingi „JavaScript“ kodai, norint gauti pažymėtų žymimųjų laukelių vertę

„JavaScript“ kodas, kad gautumėte visas pažymėtas žymimųjų laukelių vertes

 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;); } } 

Taip pat galite naudoti toliau pateiktą kodą, kad gautumėte visas pažymėtų žymimųjų laukelių reikšmes.

 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;); } } 

Taigi, žymės langelio elementai leidžia pasirinkti kelis. Tai reiškia, kad vartotojai gali pasirinkti vieną ar daugiau pasirinkimų, apibrėžtų HTML formoje. Netgi galite pažymėti visus žymimuosius laukelius. Aukščiau pateiktame pavyzdyje tai jau matėte.

csv failų skaitymas java