Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
313 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 Reply

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...