我有一个带有下面代码的 boostrap 开关。它在视觉上工作正常
<div class="btn-group" data-toggle="buttons">
<label class="btn btn-primary active">
<input type="radio" name="options" id="showBuildQuantity" autocomplete="off" checked>Build Quantity
</label>
<label class="btn btn-primary">
<input type="radio" name="options" id="showNumberOfScreens" autocomplete="off" onclick="showBuildQuantity()">Number of Screens
</label>
</div>
在它下面我有一些代码来处理点击事件:
$(document).ready(function () {
$('#showBuildQuantity').on('click', function () {
<code here>
});
$('#showNumberOfScreens').on('click', function () {
<code here>
});
});
我遇到的问题是,当我点击切换按钮时,点击事件永远不会运行。
我在同一页面上有其他按钮使用相同类型的 jQuery 单击事件,它们没有问题。
最佳答案
经过一番研究后,我发现我需要使用 change 而不是 click。所以代码应该是这样的:
$('#showBuildQuantity').on('change', function () {
if (myChart != null) {
myChart.destroy();
}
setChart(getBuildQuantityData);
});
$('#showNumberOfScreens').on('change', function () {
if (myChart != null) {
myChart.destroy();
}
setChart(getNumScreensData);
});
关于javascript - 将 Click 事件附加到 Bootstrap Toggle 单选按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38645324/