亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在左側標記 onclick 復選框

在左側標記 onclick 復選框

慕容森 2023-04-27 16:48:42
我有這個燙發管理表格來為用戶設置燙發。我的想法是:選擇“管理”時,查看/編輯/借出也會被選中并被禁用。如果任何其他人被選中,則左側的其余部分也應被選中和禁用。這個想法是用戶不能選擇編輯但不能查看,或者借出但不能編輯......等等......但是如果例如編輯被選中,則借出不會而視圖會。代碼片段:$('.manage_check').on('change',function(){   $(this).parents('.perms-group').find('input[type="checkbox"]:not(".manage_check")').click();   $(this).parents('.perms-group').find('input[type="checkbox"]:not(".manage_check")').prop("disabled",true); });問題是:選中某個部分的“管理”時,如何設置禁用所有權限?選擇編輯或借出時如何檢查和禁用視圖?。第二個項目符號解釋:If you select "view" -> View gets selected as normal.If you select "edit" -> view gets selected and disabled.If you select "Lend" -> view/edit get selected and disabled.If you select "Manage" -> view/edit/lend get selected and disabled.它也應該是可切換的,所以當取消選擇它時,它會取消選中并啟用切換器。此外,由于選擇函數執行 .click(),如果您選擇視圖然后選擇“管理”,它會取消選中視圖并選中編輯。選擇“管理”時,它應該始終檢查,而不是.clickPD:這來自我今天發表的另一篇文章,但由于我已經關閉了它,所以最好用新問題創建一個新文章。
查看完整描述

1 回答

?
MMTTMM

TA貢獻1869條經驗 獲得超4個贊

您可能需要調整此解決方案以適合您當前的標記。我試圖讓它看起來更容易理解。


假設您使用此標記:


<section id="users">

? <h3>

? ? Users

? </h3>

? <label>View</label>

? <input type="checkbox" value="view" data-group="users" />

? <label>Edit</label>

? <input type="checkbox" value="edit" data-group="users" />

? <label>Manage</label>

? <input type="checkbox" value="manage" data-group="users" />

</section>

<section id="library">

? <h3>

? ? Library

? </h3>

? <label>View</label>

? <input type="checkbox" value="view" data-group="library" />

? <label>Edit</label>

? <input type="checkbox" value="edit" data-group="library" />

? <label>Lend</label>

? <input type="checkbox" value="lend" data-group="library" />

? <label>Manage</label>

? <input type="checkbox" value="manage" data-group="library" />

</section>

<section id="textBooks">

? <h3>

? ? Text Books

? </h3>

? <label>View</label>

? <input type="checkbox" value="view" data-group="textBooks" />

? <label>Edit</label>

? <input type="checkbox" value="edit" data-group="textBooks" />

? <label>Lend</label>

? <input type="checkbox" value="lend" data-group="textBooks" />

? <label>Manage</label>

? <input type="checkbox" value="manage" data-group="textBooks" />

</section>

<section id="teacherBooks">

? <h3>

? ? Teacher Books

? </h3>

? <label>View</label>

? <input type="checkbox" value="view" data-group="teacherBooks" />

? <label>Edit</label>

? <input type="checkbox" value="edit" data-group="teacherBooks" />

? <label>Lend</label>

? <input type="checkbox" value="lend" data-group="teacherBooks" />

? <label>Manage</label>

? <input type="checkbox" value="manage" data-group="teacherBooks" />

</section>

這里的關鍵是將復選框鏈接到組中。我用了一個data-group屬性。


然后你可以在你的 JavaScript (jQuery) 代碼中包含這些行:


$(function() {

? // By having a set of rules defined like this it makes it easier for you to expand them

? const ruleSet = [{

? ? ? checked: "manage",

? ? ? checkAndDisable: ["view", "edit", "lend"]

? ? },

? ? {

? ? ? checked: "view",

? ? ? checkAndDisable: []

? ? },

? ? {

? ? ? checked: "edit",

? ? ? checkAndDisable: ["view"]

? ? },

? ? {

? ? ? checked: "lend",

? ? ? checkAndDisable: ["view", "edit"]

? ? }

? ];


? const toggleElementsCheck = (elements, toggle) => {

? ? elements.each(function() {

? ? ? $(this).prop("checked", toggle);

? ? })

? };


? const toggleElementsDisable = (elements, toggle) => {

? ? elements.each(function() {

? ? ? $(this).prop("disabled", toggle);

? ? })

? };


? $("input[type=checkbox]").on("click", function() {

? ? // Which data-group the clicked checkbox belongs into?

? ? const group = $(this).data("group");


? ? // What are the other checkboxes in this group?

? ? const checkboxes = $("input[data-group=" + group + "]").not($(this));


? ? // What's the value of the clicked checkbox?

? ? const value = $(this).val();


? ? // What are the rules for this group of checkboxes?

? ? const rule = ruleSet.find(x => x.checked === value);


? ? // What are the values of the checkboxes that need manipulation on this group?

? ? const checkAndDisable = rule.checkAndDisable;


? ? // Does this click represents a "check" or an "uncheck"?

? ? const isChecked = $(this).is(":checked");


? ? // Loop through the checkboxes in this group and manipulate them accordingly

? ? $(checkboxes).each(function() {

? ? ? let value = $(this).val();

? ? ? let toggle = checkAndDisable.includes(value);


? ? ? toggleElementsCheck($(this), isChecked ? toggle : false);

? ? ? toggleElementsDisable($(this), isChecked ? toggle : false);

? ? });

? });

});


查看完整回答
反對 回復 2023-04-27
  • 1 回答
  • 0 關注
  • 117 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號