Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
205 views
in Technique[技术] by (71.8m points)

javascript - Checbox Limiter disabled problems

<input type="checkbox" value="1" name="malzeme1[]" checked="" disabled="" id="checkboxExample1" onclick="checkNumChecked(this, 2)">
<label for="checkboxExample1">So?an<b class="product-subtitlex"> | + 1 ?</b>
</label>
<input type="checkbox" value="3" name="malzeme1[]" id="checkboxExample3" onclick="checkNumChecked(this, 2)">
<label for="checkboxExample3">So?anx<b class="product-subtitlex"> | + 1 ?</b>
</label>

<input type="checkbox" value="4" name="malzeme1[]" id="checkboxExample4" onclick="checkNumChecked(this, 2)">
<label for="checkboxExample4">So?anc<b class="product-subtitlex"> | + 1 ?</b>
</label>

<input type="checkbox" value="5" name="malzeme1[]" id="checkboxExample5" onclick="checkNumChecked(this, 2)">
<label for="checkboxExample5">So?anxc<b class="product-subtitlex"> | + 1 ?</b>
</label>

<script>
  function checkNumChecked(ele, limit) {
    var ct = 0,
      siblings = document.getElementsByName(ele.name),
      checked = 0;
    for (ct = 0; ct <= siblings.length - 1; ct++) {
      checked += (siblings[ct].checked) ? 1 : 0
    }
    for (ct = 0; ct <= siblings.length - 1; ct++) {
      siblings[ct].disabled = siblings[ct].checked ? false : (checked == limit) ? true : false
    }
  }
</script>

This is mine Checkbox Limiter. For example it ll other checkboxes going disabled when I pick 2 checkbox. But there is a problem here when I after uncheck 1 checkbox forexample. It will remove original disabled="" checkbox too. It dont have to remove original disabled="" code

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

What I have done here in order to permanently disable the check box you want the code to leave alone is to use a custom data attribute "data-permdisable". This tells the code to skip making any changes to the element.

Also, I should point out that you should have checked="checked" and disabled="disabled" as a matter of form.

<input type="checkbox" value="1" name="malzeme1[]" checked="checked" disabled="disabled" id="checkboxExample1"
    onclick="checkNumChecked(this, 2)" data-permdisable="true">
<label for="checkboxExample1">
    So?an<b class="product-subtitlex"> | + 1 ?</b>
</label>
<input type="checkbox" value="3" name="malzeme1[]" id="checkboxExample3" onclick="checkNumChecked(this, 2)">
<label for="checkboxExample3">
    So?anx<b class="product-subtitlex"> | + 1 ?</b>
</label>
<input type="checkbox" value="4" name="malzeme1[]" id="checkboxExample4" onclick="checkNumChecked(this, 2)">
<label for="checkboxExample4">
    So?anc<b class="product-subtitlex"> | + 1 ?</b>
</label>
<input type="checkbox" value="5" name="malzeme1[]" id="checkboxExample5" onclick="checkNumChecked(this, 2)">
<label for="checkboxExample5">
    So?anxc<b class="product-subtitlex"> | + 1 ?</b>
</label>
<script>
    function checkNumChecked(ele, limit) {
      var ct = 0,
      siblings = document.getElementsByName(ele.name),
      checked = 0;
        for (ct = 0; ct <= siblings.length - 1; ct++) {
            checked += ((siblings[ct].getAttribute("data-permdisable") == "true") ? 0 : ((siblings[ct].checked) ? 1 : 0))
        }
        for (ct = 0; ct <= siblings.length - 1; ct++) {
            siblings[ct].disabled = (siblings[ct].getAttribute("data-permdisable") == "true") ? 
                siblings[ct].disabled : siblings[ct].checked ? false : (checked == limit) ? true : false
        }
    }
</script> 

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...