【问题标题】:I have to Iterate thorough check boxes if the value of checkbox is in my array I want the check box to be checked如果复选框的值在我的数组中,我必须遍历复选框我希望选中复选框
【发布时间】:2022-01-05 06:50:12
【问题描述】:

这是我的功能,它不起作用,我的复选框保持未选中状态

$(document).ready(function() {
        var sales_agent_prev_arr = ['21-1', '21-2', '21-3', '21-5', '21-6', '26-1', '26-2', '26-3', '8-1', '8-9', '8-2', '8-3', '8-4', '8-5', '13']
        $('#user_type').on('change', function() {
          if ($(this).val() == 6) {
            $('input[type="checkbox"]').each(function() {
              if ($.inArray(this.value, sales_agent_prev_arr) !== -1) {
                $(this).prop('checked',true);
              }
            });
          }
        })
      });
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<ul>
  <li><input type="checkbox" value="21-1">View All</li>
  <li><input type="checkbox" value="21-2">check</li>
</ul>

【问题讨论】:

  • 请附上相关的html代码
  • 是的,抱歉我现在添加了相关代码
  • 您的代码在我运行时运行良好。请包含 id user_type 的元素并将其全部放在同一个 sn-p 中。
  • 当我在 $(this).prop('checked',true); 之后控制复选框的状态时我是真的,但我看不到复选框被选中
  • Tamilselvan,正如我所说,请包含所有相关代码。没有它,我们将无能为力。

标签: javascript jquery checkbox


【解决方案1】:

请检查您的代码的两行,没有它们它可以正常工作:

$('#user_type').on('change', function() {
      if ($(this).val() == 6) {

【讨论】:

  • 这不是答案。我敢打赌,该操作正在使用具有 iduser_type 的元素,但只是没有将其包含在问题中
  • 是的,用户类型用于某些特权条件,我需要此操作才能继续,只有用户属于该类型
猜你喜欢
  • 1970-01-01
  • 2017-01-16
  • 2021-01-08
  • 2021-03-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-12-15
  • 1970-01-01
相关资源
最近更新 更多