0

問題は次のとおりです。ページの読み込み時に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つのオプションを選択し、他のすべてのオプションを選択解除し、別のオプションを選択し、他のすべてのオプションを選択解除し ます。ただし、上記の例では、複数を選択できます

これが私が期待することのスクリーンショットです(真ん中のオプションがデフォルトで選択されていて、次に現在強調表示されているオプションを選択しました。真ん中のオプションは自動的に強調表示されていません):期待される動作

これが実際に起こることのスクリーンショットです(動的に作成されたラジオボタンを使用して):実際の動作

4

2 に答える 2

4

動的に生成される無線入力のname属性は「Product.Id」です。ただし、ラジオボタンを相互に排他的にしたい場合は、同じ名前を付ける必要があります。たとえば、メソッドを次のように変更します。

createNewRadioButton(
    '#radioX',
    product.Id,
    product.GroupByName, // ! use one identifier for mutually exclusive radio's
    product.Name,
    index
);
于 2011-11-28T18:20:11.367 に答える
0

ラジオボタンのグループを繰り返し処理してから、新しく選択したボタンを有効にするJavaScript onclick関数を追加できますか?広く普及しているJSFラジオボタンに対して同様のことを行ったので、コードは問題に正確ではありませんが、おそらく近いものが機能します。

function updateRadioButtons(radio) {
    var id = radio.name.substring(radio.name.lastIndexOf(':'));
    var el = radio.form.elements;
    for ( var i = 0; i < el.length; i++) {
        if (el[i].name.substring(el[i].name.lastIndexOf(':')) == id) {
            el[i].checked = false;
        }
    }

    radio.checked = true;
}

そして、ラジオボタンのonclickイベントは、次のように通過します。

onclick='updateRadioButtons(this);'

一緒に属するラジオボタンのグループには、命名規則が必要です。radioGroupA:button1、radioGroupA:button2などのように。

于 2011-11-28T18:13:13.577 に答える