如果单击复选框,则禁用整个复选框部分。

huangapple go评论136阅读模式
英文:

disable entire section of checkboxes if a check box is clicked

问题

我想禁用一部分复选框,如果单击“不感兴趣”复选框。以下是我的代码:

<div>
    <input style="margin-left:30px" id="notInterest" type="checkbox" name="test" onclick="Reassign(this)"> 不感兴趣
</div>

<div class="form-group row" id="reassignChecks">
   @for (var i = 1; i <= 10; i++)
        {
            <input name="AreChecked" type="checkbox" value="@i" /> @i
            <br />
        }
        <button>点击</button>
    </div>

我有以下JavaScript代码:

function Reassign(cb) {
    if (cb.checked == true) {
        document.getElementById('reassignChecks').disabled = true;
    } else {
        document.getElementById('reassignChecks').disabled = false;
    }
}

如果我点击“不感兴趣”复选框,我想禁用具有ID“reassignChecks”的整个部分,但使用上面的JavaScript,这不起作用。如果单击“不感兴趣”复选框,我不希望单击“reassignChecks”部分中的任何复选框。下面是屏幕截图:

英文:

I want to disable a section of checkboxes if the "not interested" check box is clicked. below is my code:

&lt;div&gt;
&lt;input style=&quot;margin-left:30px&quot; id=&quot;notInterest&quot; type=&quot;checkbox&quot; name=&quot;test&quot; onclick=&quot;Reassign(this)&quot;&gt; Not Interested

&lt;/div&gt;

&lt;div class=&quot;form-group row&quot; id=&quot;reassignChecks&quot;&gt;
   @for (var i = 1; i &lt;= 10; i++)

        {

            &lt;input name=&quot;AreChecked&quot; type=&quot;checkbox&quot; value=&quot;@i&quot; /&gt; @i

            &lt;br /&gt;

        }

        &lt;button&gt;Click&lt;/button&gt;

            &lt;/div&gt; 

I have the following JavaScript:

function Reassign(cb) {

            if (cb.checked == true) {

                document.getElementById(&#39;reassignChecks&#39;).disabled = true;

            } else {

                document.getElementById(&#39;reassignChecks&#39;).disabled = false;

            }

        }

if I click on "Not Interested" check box then I want to disable the entire section that has an ID of "reassignChecks", but with the above JavaScript, this is not working. I dont want any of the checkboxes to be clicked in the reassignChecks section if Not Interested check box is clicked. below is the screen shot:

如果单击复选框,则禁用整个复选框部分。

答案1

得分: 1

请尝试以下代码:

function Reassign(cb) {
    if (cb.checked == true) {
        document.getElementById("reassignChecks").disabled = true;
        var nodes = document.getElementById("reassignChecks").getElementsByTagName('*');

        for (var i = 0; i < nodes.length; i++) {
            nodes[i].setAttribute('disabled', true);
        }
    } else {
        document.getElementById("reassignChecks").disabled = false;
        var nodes = document.getElementById("reassignChecks").getElementsByTagName('*');
        for (var i = 0; i < nodes.length; i++) {
            nodes[i].removeAttribute('disabled');
        }
    }
}
<div>
    <input style="margin-left:30px" id="notInterest" type="checkbox" name="test" onclick="Reassign(this)"> Not Interested
</div>

<div class="form-group row" id="reassignChecks">
    @for (var i = 1; i <= 10; i++) {
        <input name="AreChecked" type="checkbox" value="@i" /> @i
        <br />
    }
    <button>Click</button>
</div>
英文:

Try this one-

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

function Reassign(cb) {
     if (cb.checked == true) {
                document.getElementById(&quot;reassignChecks&quot;).disabled = true;
                var nodes = document.getElementById(&quot;reassignChecks&quot;).getElementsByTagName(&#39;*&#39;);

                for (var i = 0; i &lt; nodes.length; i++) {
                    nodes[i].setAttribute(&#39;disabled&#39;, true);
                }

     } else {

                document.getElementById(&quot;reassignChecks&quot;).disabled = false;
    var nodes = document.getElementById(&quot;reassignChecks&quot;).getElementsByTagName(&#39;*&#39;);
            for (var i = 0; i &lt; nodes.length; i++) {
                nodes[i].removeAttribute(&#39;disabled&#39;);
            }
      }
 }

<!-- language: lang-html -->

&lt;div&gt;
&lt;input style=&quot;margin-left:30px&quot; id=&quot;notInterest&quot; type=&quot;checkbox&quot; name=&quot;test&quot; onclick=&quot;Reassign(this)&quot;&gt; Not Interested

&lt;/div&gt;

&lt;div class=&quot;form-group row&quot; id=&quot;reassignChecks&quot;&gt;
   @for (var i = 1; i &lt;= 10; i++)

        {

            &lt;input name=&quot;AreChecked&quot; type=&quot;checkbox&quot; value=&quot;@i&quot; /&gt; @i

            &lt;br /&gt;

        }

        &lt;button&gt;Click&lt;/button&gt;

            &lt;/div&gt; 

<!-- end snippet -->

答案2

得分: 1

&lt;!-- 开始代码段: js 隐藏: false 控制台: true Babel: false --&gt;

&lt;!-- 语言: lang-js --&gt;

function Reassign(cb) {
          
      
      if (cb.checked)
      {
         
         $(&#39;#reassignChecks&#39;).addClass(&#39;disableSection&#39;);

      }
      else
      {
          $(&#39;#reassignChecks&#39;).removeClass(&#39;disableSection&#39;);
      }

}


&lt;!-- 语言: lang-css --&gt;

.disableSection{
  pointer-events: none;
}

&lt;!-- 语言: lang-html --&gt;

&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js&quot;&gt;&lt;/script&gt;
&lt;div&gt;
  &lt;input style=&quot;margin-left:30px&quot; id=&quot;notInterest&quot; type=&quot;checkbox&quot; name=&quot;test&quot; onclick=&quot;Reassign(this)&quot;&gt; 不感兴趣
&lt;/div&gt;

&lt;div class=&quot;form-group row&quot; id=&quot;reassignChecks&quot; &gt;
   @for (var i = 1; i &lt;= 10; i++)
    {
      &lt;input name=&quot;AreChecked&quot; type=&quot;checkbox&quot; value=&quot;@i&quot; /&gt; @i&lt;br /&gt;

    }

 &lt;button&gt;点击&lt;/button&gt;
&lt;/div&gt; 

&lt;!-- 结束代码段 --&gt;

请尝试这段代码。
英文:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

function Reassign(cb) {
        
        
      if (cb.checked)
      {
         
         $(&#39;#reassignChecks&#39;).addClass(&#39;disableSection&#39;);

      }
      else
      {
          $(&#39;#reassignChecks&#39;).removeClass(&#39;disableSection&#39;);
      }

}

<!-- language: lang-css -->

.disableSection{
  pointer-events: none;
}

<!-- language: lang-html -->

&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js&quot;&gt;&lt;/script&gt;
&lt;div&gt;
  &lt;input style=&quot;margin-left:30px&quot; id=&quot;notInterest&quot; type=&quot;checkbox&quot; name=&quot;test&quot; onclick=&quot;Reassign(this)&quot;&gt; Not Interested
&lt;/div&gt;

&lt;div class=&quot;form-group row&quot; id=&quot;reassignChecks&quot; &gt;
   @for (var i = 1; i &lt;= 10; i++)
    {
      &lt;input name=&quot;AreChecked&quot; type=&quot;checkbox&quot; value=&quot;@i&quot; /&gt; @i&lt;br /&gt;

    }

 &lt;button&gt;Click&lt;/button&gt;
&lt;/div&gt; 

<!-- end snippet -->

Try this code.

答案3

得分: 0

First: 对于一个 div 元素,它没有一个 disable 属性供您控制禁用;

Second: 您可以通过向 div 添加或移除 CSS 类来实现此功能,CSS 属性是 pointer-events

.disable-region{
  pointer-events: none;
}

以及其他颜色属性来模拟禁用功能。

英文:

First:For a div dom ,it has not a disable attribute for you to control disable;

Second: You should realize this by add or remove a css from the div,the css property is pointer-events

.disable-region{
  pointer-events: none;
}

and other color properties to imitate the disable function

huangapple
  • 本文由 发表于 2023年3月9日 13:24:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/75680722.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定