問題は次のとおりです。ページの読み込み時にX個のラジオボタンのリストを動的に作成します。リストを作成して表示したら、ユーザーが一度に1つだけ選択できるようにします(通常のラジオボタンのグループのように)。ただし、ユーザーは、ラジオボタンのグループとしてではなく、個別に機能しているかのように、複数のオプションを選択できます。
これが私がドキュメントレディで呼び出すコードです:
// On document ready...
$(document).ready(function() {
// add click events to any inputs to the radio btn div, setup jQuery UI radio buttonset...
$("#radioX").on("click", "input", displayProductDetails).buttonset();
// get all the products
$.get(
"php/getProducts.php",
function(responseData){
// set the products array
products = responseData;
// setup the radio buttons for the products returned
setupProductRadioButtons(products);
},
"json"
);
});
ラジオボタンを作成するコードは次のとおりです。
// method used to setup the list of selectable products based on the list returned
// from the server
function setupProductRadioButtons(products) {
// create a radio button for each element in the products array
jQuery.each(products, function(index) {
// get the product from the array using the current index
var product = products[index];
// create a new radio button using the product name
createNewRadioButton(
'#radioX',
product.Id,
product.Id,
product.Name,
index
);
});
// refresh the button set - jQuery UI
$("#radioX").buttonset("refresh");
}
// function used to create a new radio button
function createNewRadioButton(
selector,
newRadioBtnId,
newRadioBtnName,
newRadioBtnText,
newRadioBtnValue){
// create a new radio button using supplied parameters
var newRadioBtn = $('<input />').attr({
type: "radio", id: newRadioBtnId, name: newRadioBtnName, value: newRadioBtnValue
});
// create a new label and append the new radio button
var newLabel = $('<label />').attr('for', newRadioBtnId).text(newRadioBtnText);
// add the input then the label (and forget about the <br/>
$(selector)
.append(newRadioBtn) // add the radio button
.append(newLabel) // add the label
.append("<br>"); // add a line break to separate the buttons
}
HTML(動的に作成されたラジオボタンを除く)は次のとおりです。
<div id="leftMenu">
<form>
<div id="radioX">
</div>
</form>
</div>
何か案は?jQuery UIラジオボタンのロードを手動で設定すると、すべてが期待どおりに機能します。1つのオプションを選択し、他のすべてのオプションを選択解除し、別のオプションを選択し、他のすべてのオプションを選択解除し ます。ただし、上記の例では、複数を選択できます。
これが私が期待することのスクリーンショットです(真ん中のオプションがデフォルトで選択されていて、次に現在強調表示されているオプションを選択しました。真ん中のオプションは自動的に強調表示されていません):期待される動作
これが実際に起こることのスクリーンショットです(動的に作成されたラジオボタンを使用して):実際の動作