首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >当我选择一个input="radio“时启用/禁用某些复选框

当我选择一个input="radio“时启用/禁用某些复选框
EN

Stack Overflow用户
提问于 2020-01-26 03:16:42
回答 2查看 58关注 0票数 2

我想启用/禁用一些checkbox当我选择一个input="radio"时,读取这个post似乎很容易,但并不起作用,因为不是什么都不做,它们总是被禁用。我失败了什么?

代码语言:javascript
复制
$("#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);
  }
});
代码语言:javascript
复制
<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>

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-01-26 04:00:32

我使用的是名为checkboxradiojquery UI小部件。对于启用和禁用,我必须使用their own methods

代码语言:javascript
复制
$( ".selector" ).checkboxradio( "enable" );
$( ".selector" ).checkboxradio( "disable" );

它看起来像这样:

代码语言:javascript
复制
$("#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" );
    }
});

感谢每一个人!

票数 -1
EN

Stack Overflow用户

发布于 2020-01-26 03:34:30

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

则您的代码仅在第一个按钮处进行选择。当我们点击另一个按钮时,逻辑上不会发生任何事情。因此,我们选择这两个单选按钮,并遵循我们的条件。

注意,我从单选按钮中删除了类filtros_mapa ...因为它也会禁用这些按钮,所以你不能切换。(除非你想一劳永逸)

代码语言:javascript
复制
$('input[type=radio]').change(function() {
  if ($('#radio_ruta').is(':checked')) {
    $(".filtros_mapa").prop("disabled", true);
  } else {
    $('.filtros_mapa').prop("disabled", false);
  }
});
代码语言:javascript
复制
<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>

票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59912614

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档