【问题标题】:How to set checked properties false on checkbox array in js如何在js中的复选框数组上将选中的属性设置为false
【发布时间】:2021-06-12 07:37:15
【问题描述】:

我的复选框

  <div class="col-md-3" style="padding-left:15 !important;">
                            <div class="icheckbox_minimal-grey checked disabled" style="position: relative;"><input class="alanlar" type="checkbox" disabled="" name="gunlukAlanlar[]" value="tarih" checked="" style="position: absolute; opacity: 0;"><ins class="iCheck-helper" style="position: absolute; top: 0%; left: 0%; display: block; width: 100%; height: 100%; margin: 0px; padding: 0px; background: rgb(255, 255, 255); border: 0px; opacity: 0;"></ins></div>
                            <label class="control-label" style="margin-left: 5px;color:red;">Tarih</label>
                            </div>
<div class="col-md-3" style="padding-left:15 !important;">
                        <div class="icheckbox_minimal-grey checked disabled" style="position: relative;"><input class="alanlar" type="checkbox" disabled="" name="gunlukAlanlar[]" value="saat" checked="" style="position: absolute; opacity: 0;"><ins class="iCheck-helper" style="position: absolute; top: 0%; left: 0%; display: block; width: 100%; height: 100%; margin: 0px; padding: 0px; background: rgb(255, 255, 255); border: 0px; opacity: 0;"></ins></div>
                        <label class="control-label" style="margin-left: 5px;color:red;">Saat</label>
                        </div>
<div class="col-md-3" style="padding-left:15 !important;">
                        <div class="icheckbox_minimal-grey checked disabled" style="position: relative;"><input class="alanlar" type="checkbox" disabled="" name="gunlukAlanlar[]" value="Order Adı" checked="" style="position: absolute; opacity: 0;"><ins class="iCheck-helper" style="position: absolute; top: 0%; left: 0%; display: block; width: 100%; height: 100%; margin: 0px; padding: 0px; background: rgb(255, 255, 255); border: 0px; opacity: 0;"></ins></div>
                        <label class="control-label" style="margin-left: 5px;color:red;">Order Adı</label>
                        </div>

JQuery 库

<script src="https://code.jquery.com/jquery-3.5.1.min.js"></script> 

JS代码

$("input[name='gunlukAlanlar[]']")[2].prop("checked", false);

但它不起作用。我希望未选中索引 2。但它不起作用。

请查看图片

【问题讨论】:

  • 对不起,由于网络问题,我无法进入一些房子。我现在尝试。但没有解决问题
  • 我更新了我的问题。请查看新图片
  • 好的。我刚刚调整了我的答案。看看这对你有没有帮助。
  • 比“不工作”更具体。怎么了?什么没有发生?你得到什么控制台错误?
  • 无法再次使用 Kinglish。并且不会出现控制台错误。

标签: javascript jquery checkboxlist


【解决方案1】:

不要在 jQuery 元素集合上引用数组索引(这无论如何都不起作用) - 坚持使用 JQ 并使用 .eq()

您的代码还将一个不可见的复选框 (opacity: 0) 设置为 false (prop("checked", false);) - 当它们都未选中时...

在此,我已将其设置为可见“选中”复选框。这只是一个 css 类,所以很容易摆脱。

$("input[name='gunlukAlanlar[]']").eq(1).prop("checked", false);
<script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
<label><input class="alanlar" type="checkbox" name="gunlukAlanlar[]" id="gunlukAlanlar[]" checked value="saat" /> //index 0</label>
<label><input class="alanlar" checked type="checkbox" name="gunlukAlanlar[]" id="gunlukAlanlar[]" value="modelii">//index 1</label>
<label><input class="alanlar" checked type="checkbox" name="gunlukAlanlar[]" id="gunlukAlanlar[]" value="tarih">//index 2</label>
<label><input class="alanlar" checked type="checkbox" name="gunlukAlanlar[]" id="gunlukAlanlar[]" value="musteri">//index 3</label>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-01-08
    • 1970-01-01
    • 1970-01-01
    • 2015-07-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-10
    相关资源
    最近更新 更多