【问题标题】:enable/disable some checkbox when I select a input="radio"当我选择 input="radio" 时启用/禁用某些复选框
【发布时间】:2023-03-23 23:34:02
【问题描述】:

当我选择input="radio" 时,我想启用/禁用一些checkbox,阅读此post 似乎很容易,但由于什么都不做而无法正常工作,它们始终仍处于禁用状态。我失败了什么?

$("#radio_ruta").on('click', function() {
  if ($('#radio_ruta').is(':checked')) {
    $(".filtros_mapa").removeAttr("disabled");
  } else {
    $('.filtros_mapa').checkboxradio('disabled');
    //$(".filtros_mapa").attr("disabled", true);
    //$(".filtros_mapa").prop("disabled", true);
  }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>


<div class="container-fluid">
  <div class="row" id="map_section">
    <div class="col-2 input" id="filter_container">
      <!-- FILTROS-->
      <legend>Filtros mapa: </legend>
      <label for="radio_ult_pos">Última posición</label>
      <input type="radio" name="radio_select" class="filtros_mapa" id="radio_ult_pos">
      <label for="radio_ruta">Ruta</label>
      <input type="radio" name="radio_select" class="filtros_mapa" id="radio_ruta">

      <legend>Filtro datos: </legend>
      <label for="checkbox-1">Todos</label>
      <input type="checkbox" name="checkbox-1" class="filtros_mapa filtros_mapa_ruta" id="checkbox-1" disabled>
      <label for="checkbox-2">tracker 1</label>
      <input type="checkbox" name="checkbox-2" class="filtros_mapa filtros_mapa_ruta" id="checkbox-2" disabled>
      <label for="checkbox-3">tracker 2</label>
      <input type="checkbox" name="checkbox-3" class="filtros_mapa filtros_mapa_ruta" id="checkbox-3" disabled>
      <label for="checkbox-4">tracker 3</label>
      <input type="checkbox" name="checkbox-4" class="filtros_mapa filtros_mapa_ruta" id="checkbox-4" disabled>

      <input type="number" class="form-control filtros_mapa_ruta" id='n_nodes_ruta' value="2" min="1" disabled>
      <button type="button" class="btn btn-success" id="filtrar_btn_map">Filtrar</button>
    </div>

    <!-- MAPA-->
    <div class="col-7" id="issMap">
    </div>
    <!-- INFORMACIÓN -->
    <div class="col-3" id="info">
    </div>
  </div>
</div>

【问题讨论】:

  • 我看到你正在使用来自 jQuery UI 的 .checkboxradio 方法,你确定你已经链接了库脚本吗?
  • 是的 :) @Jazzpaths
  • 嗯,这很奇怪,因为它似乎在 sn-p 中工作,当我运行它时,复选框被禁用,当我点击 Ruta 时,它们被启用。究竟是什么问题?
  • 当我在我的程序上执行它时什么都没有发生
  • 所以,如果这里可以正常工作而您的程序不能正常工作,那肯定与我们无法在此处访问的程序的其余部分相关。

标签: javascript jquery checkbox


【解决方案1】:

我正在使用名为 checkboxradiojquery UI 小部件。要启用和禁用,我必须使用their own methods

$( ".selector" ).checkboxradio( "enable" );
$( ".selector" ).checkboxradio( "disable" );

看起来像这样:

$("#radio_ult_pos").on('click', function() {
    if ($('#radio_ult_pos').is(':checked')) {
        $( ".filtros_mapa_ruta" ).checkboxradio( "disable" );
    }
});
$("#radio_ruta").on('click', function() {
    if ($('#radio_ruta').is(':checked')) {
        $( ".filtros_mapa_ruta" ).checkboxradio( "enable" );
    }
});

谢谢大家!!

【讨论】:

    【解决方案2】:

    [更新] 您在代码中使用 disabled 作为属性。使用 prop("disabled", true); 添加或删除它时也应该这样做;

    那么您的代码只会在第一个按钮处进行选择。当我们点击另一个按钮时,逻辑上什么都不会发生。因此,我们选择两个单选按钮并遵循我们的条件。

    请注意,我从单选按钮中删除了 filtros_mapa 类...因为它也禁用了这些按钮,因此您将无法切换。 (除非你想一劳永逸)

    $('input[type=radio]').change(function() {
      if ($('#radio_ruta').is(':checked')) {
        $(".filtros_mapa").prop("disabled", true);
      } else {
        $('.filtros_mapa').prop("disabled", false);
      }
    });
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <div class="container-fluid">
      <div class="row" id="map_section">
        <div class="col-2 input" id="filter_container">
          <!-- FILTROS-->
          <legend>Filtros mapa: </legend>
          <label for="radio_ult_pos">Última posición</label>
          <input type="radio" name="radio_select" class="" id="radio_ult_pos">
          <label for="radio_ruta">Ruta</label>
          <input type="radio" name="radio_select" class="" id="radio_ruta">
    
          <legend>Filtro datos: </legend>
          <label for="checkbox-1">Todos</label>
          <input type="checkbox" name="checkbox-1" class="filtros_mapa filtros_mapa_ruta" id="checkbox-1" disabled>
          <label for="checkbox-2">tracker 1</label>
          <input type="checkbox" name="checkbox-2" class="filtros_mapa filtros_mapa_ruta" id="checkbox-2" disabled>
          <label for="checkbox-3">tracker 2</label>
          <input type="checkbox" name="checkbox-3" class="filtros_mapa filtros_mapa_ruta" id="checkbox-3" disabled>
          <label for="checkbox-4">tracker 3</label>
          <input type="checkbox" name="checkbox-4" class="filtros_mapa filtros_mapa_ruta" id="checkbox-4" disabled>
    
          <input type="number" class="form-control filtros_mapa_ruta" id='n_nodes_ruta' value="2" min="1" disabled>
          <button type="button" class="btn btn-success" id="filtrar_btn_map">Filtrar</button>
        </div>
    
        <!-- MAPA-->
        <div class="col-7" id="issMap">
        </div>
        <!-- INFORMACIÓN -->
        <div class="col-3" id="info">
        </div>
      </div>
    </div>

    【讨论】:

    • 什么也没发生 :(
    • 现在应该可以工作了!我匆忙使用了您的代码:)您也在为单选按钮使用相同的类并且只选择一个按钮...
    猜你喜欢
    • 1970-01-01
    • 2020-01-28
    • 2013-11-17
    • 2016-02-22
    • 1970-01-01
    • 2018-01-16
    • 2015-08-02
    • 2013-04-19
    • 2020-10-21
    相关资源
    最近更新 更多