Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
306 views
in Technique[技术] by (71.8m points)

javascript - How to make your checkbox work with your Jquery functionality?

I have created 3 checkbox and i am struggling to make them to communicate with my Jquery button i have created to allow a user an option to select. If one is selected and other one both are, they must when button is clicked should download my feeds as CSV file. I have the logic below i want to share for clear understanding as to what exactly i want to achieve.

$(document).ready(function() {
  $("#download").click(function() {
    window.location.href = 'https://api.thingspeak.com/channels/899906/feeds.csv?start=2019-11-12%2019:11:12&end=2019-11-13%2019:11:13';
  });

});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<div class="custom-control custom-checkbox">
  <input type="checkbox" class="custom-control-input" id="temperature">
  <label class="custom-control-label" for="temperature">Temperature</label>
</div>

<div class="custom-control custom-checkbox">
  <input type="checkbox" class="custom-control-input" id="illuminance">
  <label class="custom-control-label" for="illuminance">Illuminance</label>
</div>
<div class="custom-control custom-checkbox">
  <input type="checkbox" class="custom-control-input" id="button-state">
  <label class="custom-control-label" for="button-state">Button-State</label>

  <!---Downloading File using 
    Jquery with Buttons---->
  <div class="form-group"><br>
    <div class="col-md-1.9 text-center">
      <button id="download" name="download" class="btn btn-warning">Download</button><br>
    </div>
  </div>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You want to initiate the download if at least two checkboxes are checked?

In that case, try using this:

$(document).ready(function() {
    $("#download").click(function() {
        var count = 0;
        if ($('#temperature').prop('checked')) count++;
        if ($('#illuminance').prop('checked')) count++;
        if ($('#button-state').prop('checked')) count++;
        if (count > 1) {
            window.location.href ='https://api.thingspeak.com/channels/899906/feeds.csv?start=2019-11-12%2019:11:12&end=2019-11-13%2019:11:13';
        }
    });
});

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...