iT邦幫忙

0

請問如果自由設置disable和解除disable

一開始我把checkBoxOne 設置disable,onclick button 就會解除,請問怎樣知道當刻的分checkbox是否有disable再去決定 add or remove,if(????){} else if (?????????){

<button onclick="myFunction()">Button<button>

<script>
    $(".checkBoxOne").addClass("disabled").prop("disabled", true);
    
   function myFunction() {
        $(".checkBoxOne").removeClass("disabled").prop("disabled", false);
    }
    </script>
看更多先前的討論...收起先前的討論...
你有對disabled class做特別的css嗎?
$(".checkBoxOne").toggleClass('disabled');
nick12345 iT邦新手 4 級 ‧ 2021-11-18 11:14:17 檢舉
沒有class,我意思是IF ELSE 不懂寫,我ONCLICK是可以解除的,但再按多一次想做設置
照你原本的寫
$(".checkBoxOne").removeClass("disabled").prop("disabled", false);
改成我上面那句就可以了
Mao iT邦新手 1 級 ‧ 2021-11-19 13:31:22 檢舉
請問這個範例有解決你的問題嗎?
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 個回答

1
Mao
iT邦新手 1 級 ‧ 2021-11-18 13:51:55

Demo

$(".checkBoxOne").addClass("disabled").prop("disabled", true);
    
function myFunction() {
  // 先把這個節點取出來,方便操作
  const checkBoxOne = $(".checkBoxOne");
  
  /* 其實不用使用判斷式,樣式的部分使用 toggleClass,
   * disabled 的部分,直接把寫死的布林值變成取值後反轉就可以
   * 如: !checkBoxOne.prop("disabled")
   */
  checkBoxOne
    .toggleClass("disabled")
    .prop("disabled", !checkBoxOne.prop("disabled"));
}

我要發表回答

立即登入回答