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

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

使用復選框啟用/禁用按鈕

使用復選框啟用/禁用按鈕

喵喵時光機 2023-02-24 10:36:28
如果兩個復選框都被選中,我需要啟用/禁用按鈕。我想在 knockout.js 中做。我還是個初學者。我找到了一個使用復選框啟用/禁用按鈕的示例,但這是針對 1 個復選框執行的。在我的場景中,我有兩個復選框,應該選中它們以啟用按鈕。如果未選中任何復選框,則應禁用該按鈕。<input type="checkbox" data-bind="checked: myBoolean" /><button data-bind="enable: myBoolean">My Button</button>ko.applyBindings({ myBoolean: ko.observable(true) });任何幫助或建議將不勝感激。提前致謝
查看完整描述

2 回答

?
至尊寶的傳說

TA貢獻1789條經驗 獲得超10個贊

你可以

  1. 為另一個復選框添加另一個可觀察對象

  2. 為按鈕添加一個計算的可觀察值,只有當兩個復選框都被選中時才返回真(可觀察值是真的)

var test = {?

? myBoolean1: ko.observable(false),

? myBoolean2: ko.observable(false)

};

test.myComputed = ko.computed(function() { return test.myBoolean1() && test.myBoolean2() });


ko.applyBindings(test);

<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.4.2/knockout-min.js"></script>

<input type="checkbox" data-bind="checked: myBoolean1" />

<input type="checkbox" data-bind="checked: myBoolean2" />

<button data-bind="enable: myComputed">My Button</button>


查看完整回答
反對 回復 2023-02-24
?
呼喚遠方

TA貢獻1856條經驗 獲得超11個贊

您只能使用 CSS 來完成此操作。除非你需要支持 IE10 或更低版本。


button {

  cursor: not-allowed;

  pointer-events: none;

  color: grey;

  background-color: white;

}


#firstCheckbox:checked+#secondCheckbox:checked+button {

  color: black;

  cursor: pointer;

  pointer-events: auto;

}

<input type="checkbox" id="firstCheckbox">

<input type="checkbox" id="secondCheckbox">

<button>Click me</button>


查看完整回答
反對 回復 2023-02-24
  • 2 回答
  • 0 關注
  • 187 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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