如何使您的复选框与Jquery功能配合使用



我已经创建了3个复选框,我正在努力让它们与我创建的Jquery按钮通信,以便用户可以选择。若一个被选中,另一个都被选中,他们必须在点击按钮时将我的提要下载为CSV文件。我有下面的逻辑,我想分享,以清楚地理解我到底想实现什么。

$(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>

如果选中了至少两个复选框,是否要启动下载?

在这种情况下,请尝试使用以下方法:

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

最新更新