ボタンセットの組み込みイベントbuttonsetcreate
を使用した非常にエレガントなソリューションがあります。または、ボタンセットのコールバック イベントを使用できます。ここで説明されているすべてhttps://api.jqueryui.com/buttonset/#event-create
このイベントにより、ボタンセットが開始されるたびに、好きなことを行うことができます。初期化されているかどうかを確認するために何度も確認する必要はありません。コードを入力するだけevent
で完了です。
フィドル
https://jsfiddle.net/ergec/wk9ew1bp/
html
<fieldset>
<legend>Favorite jQuery Project</legend>
<div id="radio">
<input type="radio" id="sizzle" name="project">
<label for="sizzle">Sizzle</label>
<input type="radio" id="qunit" name="project" checked="checked">
<label for="qunit">QUnit</label>
<input type="radio" id="color" name="project">
<label for="color">Color</label>
</div>
</fieldset>
<button id="initbuttonset">Init</button>
<button id="checkbuttonset">Check</button>
js
var isbuttonsetinit = false;
$("#radio").on("buttonsetcreate", function(event, ui) {
isbuttonsetinit = true;
alert("buttonset init");
});
$("#initbuttonset").click(function() {
$("#radio").buttonset();
});
$("#checkbuttonset").click(function() {
alert(isbuttonsetinit);
});
js (代替)
var isbuttonsetinit = false;
$("#initbuttonset").click(function() {
$("#radio").buttonset({create: function( event, ui ) {
isbuttonsetinit = true;
alert("buttonset init");
}});
});
$("#checkbuttonset").click(function() {
alert(isbuttonsetinit);
});