jquery - 如何限制两个字段的选择不覆盖其他复选框?
问题描述
我为复选框创建了两个字段,但我正在努力限制我的复选框。这两个目前做什么,他们允许进行选择。但是每次我点击下载按钮时。它允许其他复选框自动下载,我不希望用户在前端看到它。下面是我的逻辑;
<!---- Checkbox for selecting it to download. -->
<div class = "custom-control custom-checkbox">
<input type="checkbox" class ="custom-control-input" id="acceleration">
<label class = "custom-control-label" for="acceleration">Acceleration</label>
</div>
<!---checkbox for button-state showing on/off when clicked. ---->
<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>
</div>
<!----Download button into CSV file. --->
<div class="form-group">
<div class="col-md-1.9 text-center">
<button id="download" name="download" class="btn btn-warning" >Download</button><br>
</div>
</div>
Javascript:
$(function(){
$("#download").click(function(e) {
e.preventDefault();
var isAccelerate = ($('#acceleration').is(':checked'));
var isButtonState = ($('#button_state').is(':checked'));
if (isAccelerate&&isButtonState) {
window.open('https://api.thingspeak.com/channels/932891/feeds.csv?api_key=**&start=2019-12-13T00:00+02&end=2019-12-17T23:59+02:00');
}
if (isAccelerate){
window.open('https://api.thingspeak.com/channels/932891/fields/1.csv?api_key=***&start=2019-12-13T00:00+02&end=2019-12-17T23:59+02:00');
}
if (isButtonState){
window.open('https://api.thingspeak.com/channels/932891/fields/8.csv?api_key=***&start=2019-12-13T00:00+02&end=2019-12-17T23:59+02:00');
}
});
});
JSFiddle:链接
解决方案
根据 Freedomm-m。
<div class = "custom-control custom-checkbox">
<input type="checkbox" class ="custom-control-input" id="acceleration">
<label class = "custom-control-label" for="acceleration">Acceleration</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>
</div>
<div class="form-group"><br>
<div class="col-md-1.9 text-center">
<button type="button" id="download" name="download" class="btn btn-warning" >Download</button><br>
</div>
</div>
$(function(){
$("#download").click(function(e) {
e.preventDefault();
var isAccelerate = ($('#acceleration').is(':checked'));
var isButtonState = ($('#button_state').is(':checked'));
if(isAccelerate && isButtonState) {
window.open('https://api.thingspeak.com/channels/932891/feeds.csv?api_key=**&start=2019-12-13T00:00+02&end=2019-12-17T23:59+02:00');
}else{
if(isAccelerate){
window.open('https://api.thingspeak.com/channels/932891/fields/1.csv?api_key=***&start=2019-12-13T00:00+02&end=2019-12-17T23:59+02:00');
}
if(isButtonState){
window.open('https://api.thingspeak.com/channels/932891/fields/8.csv?api_key=***&start=2019-12-13T00:00+02&end=2019-12-17T23:59+02:00');
}
}
});
});
演示:链接
推荐阅读
- spring-boot - Spring Boot gradle flyway - 验证错误 - 模式验证:缺少表
- python - 如何复制 Pandas 数据框中的每一行?
- django - 让 cloudinary 默认图像与 Django 对象一起使用
- python - RandomizedSearchCV 中的无效参数错误
- python - 如何将使用不同参数运行相同计算的代码简化为不同的输出变量?
- php - Wordpress 自定义联系表单,不知道从哪里开始
- http - Groovy Curl 意外行为
- youtube-api - 通过 Youtube API 获得的条目不计算在内
- kotlin - 将 Kotlin 内联类传递给 Retrofit 函数是否安全
- python - 保存后无法加载 Keras-Tensorflow 模型