1

标题可能有点模糊。所以让我解释一下。

我有一个页面,用户可以在其中选择他们想要预约的日期。但是,我们为他们提供了多种选择,以防第一个选择已满

单选按钮 HTML:

<input type="radio" class="my-radio" value="monday" name="my_form[day][choice1][]"
id="monday1"/>
<label for="monday1">Monday</label>

<input type="radio" class="my-radio" value="tuesday" name="my_form[day][choice1][]"
id="tuesday1"/>
<label for="tuesday1">Tuesday</label>

<input type="radio" class="my-radio" value="wednesday" name="my_form[day][choice1][]"
id="wednesday1"/>
<label for="wednesday1">Wednesday</label>

<input type="radio" class="my-radio" value="thursday" name="my_form[day][choice1][]"
id="thursday1"/>
<label for="thursday1">Thursday</label>

<input type="radio" class="my-radio" value="friday" name="my_form[day][choice1][]"
id="friday1"/>
<label for="friday1">Friday</label>

<input type="radio" class="my-radio" value="saturday" name="my_form[day][choice1][]"
id="saturday1"/>
<label for="saturday1">Saturday</label>

这又重复了两次。但是,ID 已更改,名称也已更改。所以第二个是,(星期一);id=monday2、name=my_form[day][choice2][] 等等。

所以要说清楚。3 行无线电,所有值都相同,但 id 和名称不同。

现在,当检查星期一时,无论在哪一行,我都想禁用所有其他星期一。现在,如果我改为选择星期二,则应再次启用所有星期一。

我使用 jQuery 的第一部分;

jQuery('.my-radio').change(function () {
// Get all elements associated to value, but the currently selected radio.
var associates = jQuery('.my-radio[value="' + jQuery(this).val() + '"]').not("#" + jQuery(this).attr('id'));

  // Disable every element that we found
  jQuery(associates).each(function(){
     jQuery(this).prop('disabled', true);
  });
});

现在我唯一的问题是第二部分。重新启用之前禁用的无线电。

一个 JSFiddle 让它更清楚一点; http://jsfiddle.net/Hr9h2/1/

编辑:非常感谢您的回答。

但是,我忘了提到你不能选择,例如,星期一 3 次甚至 2 次。

4

4 回答 4

7

您正在寻找的功能是通过启用所有单选按钮创建的,并遍历所有选中的单选按钮并禁用任何具有相同值的单选按钮。这样它就可以与您想要的多行单选按钮一起使用:

$('.my-radio').on('change', function () {
    $('.my-radio').prop('disabled',false).filter(':checked').each(function() {
        $('.my-radio[value="' + this.value + '"]:not(:checked)').prop('disabled', true);
    });
});

小提琴

于 2013-08-03T19:37:39.083 回答
0

认为这很简单.. 只需在禁用特定单选按钮之前启用其事件上的所有单选按钮。change

这是JS

jQuery('.my-radio').change(function () {
    jQuery('.my-radio').prop('disabled', false); //<---here
    // Disable all elements associated to value
    var associates = jQuery('.my-radio[value="' + jQuery(this).val() + '"]').not("#" + jQuery(this).attr('id'));
    associates.prop('disabled', true);
});

笔记:

您不必使用each循环来获取所有关联。该associate变量将已经包含所有元素。这是一个fiddle at jsFiddle.net

于 2013-08-03T19:33:18.157 回答
0

对代码进行更多重构:

$('.my-radio').change(function () {
    //enable all radio buttons
    $(".my-radio").prop("disabled", false);
    // Disable all elements associated to value
    $('.my-radio[value="' + this.value + '"]').not(this).prop('disabled', true);
});
于 2013-08-03T19:35:54.987 回答
0

这可能是您正在寻找的:

$('.my-radio').click(function () {
    // When a radio button is clicked
    $('.my-radio').each(function () {
        // Enables all radios buttons
        $('.my-radio').prop('disabled', false);
    });

    // Disables all radio buttons with same value as this, except this one
    $('input[value="' + $(this).val() + '"]').not(this).prop('disabled', true);

});
于 2013-08-03T19:38:03.153 回答