mercredi 28 juin 2017

if statement using jquery selectors and checkboxes

I have a form with two text input fields and a series of checkboxes.

There is a variable fields which stores some arrays with data pertaining to the selected fields. I have hardcoded the values into the script with no problem but I cannot seem to find out how to properly compose an if statement to check the status of the checkboxes and only use those values, rather than hardcoding the selected fields.

var start, end, fields;

$(function() {
  $("#form1").submit(function(event) {
    var endD = $("#endDate").val();
    end = endD;
    var startD = $("#startDate").val();
    start = startD;

    //fields = ['PM1','PM2.5','PM10'];


    //if $("#PM1").is("checked"))){
    //fields = ["PM1"];
    //}


    if ($("PM1").attr("checked")) {
      fields = ["PM1"];
    }


    event.preventDefault();

    build_graph();
  });
});
<script src="http://ift.tt/1oMJErh"></script>
<form id="form1">
  Start Datetime:<br>
  <input type="text" name="startDate" id="startDate" class="Date" placeholder="YYYYMMDD-HHMM"><br> End Datetime <br>
  <input type="text" name="startDate" id="endDate" class="Date" placeholder="YYYYMMDD-HHMM"><br> Parameter
  <br>
  <input type="checkbox" class="parameter" id="PM1">PM1<br>
  <input type="checkbox" class="parameter" id="PM2.5" checked>PM2.5<br>
  <input type="checkbox" class="parameter" id="PM10">PM10<br>
  <input type="checkbox" class="parameter" id="Temp">Temperature<br>
  <input type="checkbox" class="parameter" id="Humidity">Humidity<br>
  <input type="checkbox" class="parameter" id="Pressure">Pressure<br>
  <input type="checkbox" class="parameter" id="WindSpeed">Wind Speed<br>
  <input type="checkbox" class="parameter" id="Direction">Direction<br>
  <input type="checkbox" class="parameter" id="RainVolume">Rain Volume<br>
  <input type="submit">
</form>

Aucun commentaire:

Enregistrer un commentaire