[英]How to programmatically check a Bootstrap button group (radio) button
<div class="btn-group" data-toggle="buttons">
<label class="btn btn-sm btn-primary success active">
<input type="radio" name="options" id="optionFalse" checked />false
</label>
<label class="btn btn-sm btn-primary danger">
<input type="radio" name="options" id="optionTrue" />true
</label>
</div>
$('#optionTrue').button('toggle');
我已經閱讀了更多的問題答案,但沒有一個有效。 按鈕切換不起作用,我嘗試添加/刪除“活動”類,但沒有效果。 我正在使用 1.10 jQuery 和 3.1 Bootstrap。 這應該很簡單,我正在拔頭發!
需要在具有類btn
的元素上調用button()
...
$('#optionTrue').closest('.btn').button('toggle');
這將正確切換按鈕並正確更新每個輸入的檢查屬性......
bootstrap docs (3.2) 沒有讓這更明顯,這很瘋狂,但這是我發現的在代碼中設置單選按鈕初始狀態的最簡單方法。
<div class="btn-group" data-toggle="buttons">
<label id="optionFalse" class="btn btn-primary">
<input type="radio" name="options" /> false
</label>
<label id="optionTrue" class="btn btn-primary ">
<input type="radio" name="options" /> true
</label>
</div>
if (initTrue) {
$('#optionTrue').button('toggle');
}
else {
$('#optionFalse').button('toggle');
}
在閱讀了關於這個主題的數十篇文章后,似乎這些是一般的混淆點:
document.querySelector(".btn-group input[id='optionTrue']").checked = true;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.