我有5個複選框,如果第一個複選框被選中,那麼其他四個複選框也應該被選中。另外,如果我取消選中第一個複選框,則應禁用這些複選框。當我這樣做,其他所有的複選框被啓用,未選中,所以我執行下面的代碼:.checked和.disable在javascript中不能一起工作
<div class="total_line">
<label for="name">Beheerder </label>
<%= f.check_box:is_admin, {:onchange => "showHideDiv()"} %>
</div>
<div class="total_line">
<label for="onderwerp">Documenten beheren</label>
<%= f.check_box:document_management %>
</div>
<div class="total_line">
<label for="onderwerp">Nieuws beheren</label>
<%= f.check_box:news_management %>
</div>
<div class="total_line">
<label for="onderwerp">Agenda beheren</label>
<%= f.check_box:agenda_management %>
</div>
<div class="total_line">
<% if photo_book == true %>
<label for="onderwerp">Fotoboek beheren </label>
<%= f.check_box:photobok_admin %>
<% end %>
</div>
<div class="total_line">
<label for="onderwerp">Actief</label>
<%= f.check_box:is_actived %>
</div>
而且下面是JavaScript
function showHideDiv() {
var mycheck = document.getElementById('membership_is_admin');
if (mycheck.checked) {
document.getElementById('membership_is_admin').setAttribute("readonly", 1)
document.getElementById('membership_photobok_admin').setAttribute("readonly", 1)
document.getElementById('membership_news_management').setAttribute("readonly", 1)
document.getElementById('membership_document_management').setAttribute("readonly", 1)
document.getElementById('membership_agenda_management').setAttribute("readonly", 1)
document.getElementById('membership_document_management').checked = true;
document.getElementById('membership_news_management').checked = true;
document.getElementById('membership_agenda_management').checked = true;
document.getElementById('membership_photobok_admin').checked = true;
jQuery('#membership_document_management').attr('disabled','true');
jQuery('#membership_news_management').attr('disabled','true');
jQuery('#membership_agenda_management').attr('disabled','true');
jQuery('#membership_photobok_admin').attr('disabled','true');
} else {
document.getElementById('membership_is_admin').setAttribute("readonly", 0)
document.getElementById('membership_photobok_admin').setAttribute("readonly", 0)
document.getElementById('membership_news_management').setAttribute("readonly", 0)
document.getElementById('membership_document_management').setAttribute("readonly", 0)
document.getElementById('membership_agenda_management').setAttribute("readonly", 0)
document.getElementById('membership_document_management').checked = false;
document.getElementById('membership_news_management').checked = false;
document.getElementById('membership_agenda_management').checked = false;
document.getElementById('membership_photobok_admin').checked = false;
document.getElementById('membership_document_management').disabled = false;
document.getElementById('membership_news_management').disabled = false;
document.getElementById('membership_agenda_management').disabled = false;
document.getElementById('membership_photobok_admin').disabled = false;
}
}
的問題是其他四個複選框的值不被檢查。 如果我拿出禁用複選框的代碼,那麼它正在工作。
你應該使用['.prop('disabled',true)'](http://api.jquery.com/prop)而不是'.attr' – Blazemonger
@Blazemonger他直接使用DOM元素;沒有'.prop()'。不過,我同意這裏不需要'setAttribute()'。 – Pointy
TypeError:jQuery(「#membership_document_management」)。prop不是函數 –