Bootstrap/JQuery-单选按钮组事件函数
Bootstrap/JQuery - Radio Button Group Event Functions
我的目标是根据选择组中的单选按钮来运行函数。使用其他帖子,我能够实现下面的脚本。以下是我遇到的错误:1) 我声明默认选中的初始按钮不调用函数。如果我从按钮中删除选中的属性,它的功能与其他按钮相同。2) 每个按钮只能工作一次。例如,如果我单击按钮2,它将启动相应的功能。然后,当点击按钮3时,它也能成功工作。问题是当我尝试返回到按钮2时。它不会再开火了。
查询
$("#btn-group-data :input").on("change",function () {
switch (this.value) {
case "radar":
console.log(this.value);
//do stuff...
break;
case "watches":
console.log(this.value);
//do stuff...
break;
case "warnings":
console.log(this.value);
//do stuff...
break;
case "temp":
console.log(this.value); // points to the clicked input button
//do stuff...
break;
case "precip":
console.log(this.value); // points to the clicked input button
//do stuff...
break;
case "snow":
console.log(this.value); // points to the clicked input button
//do stuff...
break;
}
});
按钮-HTML
<div id="map-container" class="container-fluid">
<div id="map-div">
<div id="btn-group-data" data-toggle="buttons">
<label class="btn btn-primary active">
<input type="radio" name="radar" value="radar" id="radar" autocomplete="off" checked="" > Radar
</label>
<label class="btn btn-primary ">
<input type="radio" name="watches" value="watches" id="watches" autocomplete="off"> Watches
</label>
<label class="btn btn-primary">
<input type="radio" name="warnings" value="warnings" id="warnings" autocomplete="off"> Warnings
</label>
<label class="btn btn-primary">
<input type="radio" name="temp" value="temp" id="temp" autocomplete="off"> Temperature
</label>
<label class="btn btn-primary">
<input type="radio" name="precip" value="precip" id="precip" autocomplete="off"> Precipitation
</label>
<label class="btn btn-primary">
<input type="radio" name="snow" value="snow" id="snow" autocomplete="off"> Snow
</label>
</div>
</div>
</div>
您应该为所有输入提供相同的名称,如
<div id="map-div">
<div id="btn-group-data" data-toggle="buttons">
<label class="btn btn-primary active">
<input type="radio" name="radar" value="radar" id="radar" autocomplete="off" checked="" > Radar
</label>
<label class="btn btn-primary ">
<input type="radio" name="radar" value="watches" id="watches" autocomplete="off"> Watches
</label>
<label class="btn btn-primary">
<input type="radio" name="radar" value="warnings" id="warnings" autocomplete="off"> Warnings
</label>
<label class="btn btn-primary">
<input type="radio" name="radar" value="temp" id="temp" autocomplete="off"> Temperature
</label>
<label class="btn btn-primary">
<input type="radio" name="radar" value="precip" id="precip" autocomplete="off"> Precipitation
</label>
<label class="btn btn-primary">
<input type="radio" name="radar" value="snow" id="snow" autocomplete="off"> Snow
</label>
</div>
</div>
相关文章:
- 无法在java脚本中调用图像的点击事件函数
- 如何在事件函数中访问窗口实例
- 事件函数完整日历中的多个ajax调用
- 如何使参数在事件函数中工作
- Jquery Onclick 事件函数第二次不起作用
- 宣传单:双击时不要触发点击事件函数
- Bootstrap/JQuery-单选按钮组事件函数
- 将jQuery事件函数应用于新的Knockout.js数组元素
- 防止在元素上触发新事件,直到当前事件函数在jQuery中完成
- 有没有一种方法可以将多个参数传递给主干js中的事件函数
- 用javascript更新事件函数中的全局变量
- Jquery:如何在事件函数之外获取变量
- 将参数传递给事件函数
- postMessage - 多个 postMessage 事件/函数/回调
- 多事件函数中的悬停延迟
- JavaScript 事件函数 - 在声明之前调用
- 使用 jQuery 循环设置事件函数触发器
- 暂停,直到事件/函数使用 jquery 或 JavaScript 完成
- 如何访问 jQuery 事件函数中的对象属性
- 单击事件函数未从花式盒子灯箱内触发