Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Cover image for Gathering Data with Checkboxes
Saoud
Saoud

Posted on

Gathering Data with Checkboxes

Gathering Data with Checkboxes

Examples


If we had the following form containing a group of checkboxes:

transportation_survey/index.html

<!DOCTYPE html><htmllang="en-US"><head><title>Transportation Survey</title><linkhref="css/bootstrap.css"rel="stylesheet"type="text/css"><linkhref="css/styles.css"rel="stylesheet"type="text/css"><scriptsrc="js/jquery-3.5.1.js"></script><scriptsrc="js/scripts.js"></script></head><body><divclass="container"><divclass="jumbotron"><h2>Transportation Survey</h2></div><formid="transportation_survey"><divclass="form-group"><p>In the past year, I have used the following modes of transportation to travel to work or school:</p><inputtype="checkbox"name="work-transportation"value="bike"> Riding a bike.<br><inputtype="checkbox"name="work-transportation"value="car">Driving a car.<br><inputtype="checkbox"name="work-transportation"value="carpool">Carpooling with others.<br><inputtype="checkbox"name="work-transportation"value="walk">Walking.<br><inputtype="checkbox"name="work-transportation"value="bus">Riding the bus.<br><inputtype="checkbox"name="work-transportation"value="train">Riding the train.<br><inputtype="checkbox"name="work-transportation"value="streetcar">Riding the streetcar.<br><inputtype="checkbox"name="work-transportation"value="taxi">Taking a taxi.<br><inputtype="checkbox"name="work-transportation"value="rideshare">Using a rideshare service like Lyft or Uber.<br><inputtype="checkbox"name="work-transportation"value="skateboard">Skateboarding.<br><inputtype="checkbox"name="work-transportation"value="rollerblade">Rollerblading.<br><inputtype="checkbox"name="work-transportation"value="scooter">Riding a scooter.<br><inputtype="checkbox"name="work-transportation"value="other">Another mode of transportation not listed here.<br></div><buttontype="submit">Submit Survey</button></form><spanid="work-responses"><p><b>You use the following methods of transportation to travel to work or school:</b></p></span></div></body></html>
Enter fullscreen modeExit fullscreen mode

This jQuery would retrieve all selected checkboxes, and append their values to a span in our HTML:

transportation_survey/js/scripts.js

$(document).ready(function(){$("form#transportation_survey").submit(function(event){event.preventDefault();$("#work-responses").show();$("input:checkbox[name=work-transportation]:checked").each(function(){constworkTransportationMode=$(this).val();$('#work-responses').append(workTransportationMode+"<br>");});$('#transportation_survey').hide();});});
Enter fullscreen modeExit fullscreen mode
  • The input:checkbox portion of this selector targets <input> fields of the type checkbox.
  • [name=work-transportation] further narrows our search. In addition to targeting only <input> fields of the checkbox type, the name attribute of the field must also be work-transportation.
  • The :checked portion narrows it down even further. In addition to targeting only <input> fields of the checkbox type with a name attribute of work-transportation, we only want to retrieve checked checkboxes that meet these requirements.

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

DevOps / Programmer
  • Location
    Seattle, WA
  • Work
    Founding Member at a Startup
  • Joined

More fromSaoud

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp