如何使您的复选框与Jquery功能一起使用? [英] How to make your checkbox work with your Jquery functionality?
本文介绍了如何使您的复选框与Jquery功能一起使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我已经创建了3个复选框,并且我正努力使它们与我创建的Jquery按钮进行通信,以允许用户选择一个选项.如果选择了一个,而两个都选择了,则必须在单击按钮时将我的供稿下载为CSV文件.我想分享以下逻辑,以便对我要实现的目标有清晰的了解.
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>
推荐答案
如果至少选中两个复选框,您要启动下载吗?
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';
}
});
});
这篇关于如何使您的复选框与Jquery功能一起使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文