【问题标题】:jQuery Radio Buttons not switching properlyjQuery单选按钮没有正确切换
【发布时间】:2012-01-08 04:09:04
【问题描述】:

问题是:我在页面加载时动态创建 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 个选项,所有其他选项都被取消选择,我选择一个不同的选项,所有其他选项都被取消选择等等。上面的例子让我选择多个。

这是我期望发生的屏幕截图(默认选择中间选项,然后我选择当前突出显示的选项,中间选项自动取消突出显示):expected behaviour

这是实际发生的屏幕截图(使用我动态创建的单选按钮):actual behaviour

【问题讨论】:

  • 好的-刚离开办公室,我在家时会整理名字然后发布结果-我猜这肯定是问题...

标签: jquery jquery-ui radio-button jquery-ui-button


【解决方案1】:

动态生成的无线电输入的name 属性是“Product.Id”。但是,如果您希望单选按钮是互斥的,则需要为它们提供相同的名称。例如将方法更改为:

createNewRadioButton(
    '#radioX',
    product.Id,
    product.GroupByName, // ! use one identifier for mutually exclusive radio's
    product.Name,
    index
);

【讨论】:

    【解决方案2】:

    您能否添加一个 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等

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-05-20
      • 2011-04-15
      • 2013-06-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多