簡體   English   中英

即使在添加attr后也允許檢查復選框(“checked”,false)

[英]Allow checking to a checkbox even after adding attr(“checked”, false)

我不確定是否正確地制定了標題,但邏輯是這樣的。

我有4個復選框, 3個是1個 選擇3 將取消選中 ,選擇將取消選中

我的jQuery如下:

var yes = $("input[type=checkbox].yes");

if (yes.length == yes.filter(":checked").length) {
    $("#no").attr("checked", false);
} else if($("#no").is(":checked")) {
    yes.attr("checked", false);
} else {
    // do nothing
}



有用。 但是,如果我檢查了所有 ,我將無法選擇 相反,我需要取消選中1 是以選擇 同樣的事情, 沒有 如果我選中“ 否” ,則將取消選中所有是”。 但是,我需要取消選中“ 否”才能再次選擇“ 是”

如果選中是”,我是否仍然可以選擇“

謝謝!

UPDATE
現在包括HTML

<form id="checkbox-testing" >
<div class="checkform">
    <label>                        
      <input class="yes" type="checkbox" value="Yes, 1" id="yes1"><span>&nbsp;Yes, 1</span> 
    </label>
    <label>                        
      <input class="yes" type="checkbox" value="Yes, 2" id="yes2"><span>&nbsp;Yes, 2</span>
    </label>
    <label>                        
      <input class="yes" type="checkbox" value="Yes, 3" id="yes3"><span>&nbsp;Yes, 3</span>
    </label>
    <label>                        
      <input type="checkbox" value="No" id="no"><span>&nbsp;No</span>
    </label>
</div>
<input type="submit" value="Test" />
</form>

使用.prop()來測試已檢查的屬性,而不是.attr() - 以下代碼片段使用.prop然后使用.change()事件 - 在這種情況下,我只是在構造輸出但是你可以更改支柱另一個復選框。

編輯 - 我剛剛更新了我的答案,給一個無線電按鈕選擇或另一個選項和一個按鈕清除點擊的已檢查屬性。

 $(document).ready(function(){ $("input[type=radio]").change(function(){; console.log("yes: " + $(".yes").prop("checked")); console.log(" no: " + $(".no").prop("checked")); }) $('#clear').click(function(){ $("input[type=radio]").prop('checked',false) }) }) 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <label for ="yes">Yes <input type ="radio" name ="test" id="yes" class="yes" checked value"yes"/></label> <label for ="no">No <input type ="radio" name ="test" id="no" class="no" value = "no"/> <hr/> <button type ="button" name ="test" id="clear" >Clear Value</button> 

var yes = $("input[type=checkbox].yes");

if (yes.length == yes.filter(":checked").length) {
    $("#no").prop("checked", false);
} else if($("#no").is(":checked")) {
    yes.prop("checked", false);
} else {
    // do nothing
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM