可以使用切换按钮来在多于两个类之间切换。

huangapple go评论59阅读模式
英文:

It's possible to use Toggle Button to change between more than 2 classes

问题

I'm studding Java with Spring boot and Thymeleaf, so i'm creating some views and I've encountered some challenge. I wanted to create a toggle button to "Pendente" and "Recebido", but now, i'm trying to put the option "Cancelado", but I encountered some advertisements.

这是我的第一个问题,抱歉有什么问题 可以使用切换按钮来在多于两个类之间切换。

<div class="row mb-3">
    <label for="statusCodes" class="col-sm-2 col-form-label">Status</label>
    <div class="col-sm-2">
        <button type="button" id="toggleButton" class="btn btn-toggle btn-pendente" data-value="PENDENTE">
            Pendente
        </button>
        <input type="hidden" name="statusCodes" id="statusCodes" value="PENDENTE" />
    </div>
</div>
$(function () {
    $("#toggleButton").on("click", function () {
        $(this).toggleClass("btn-pendente btn-recebido btn-cancelado");
        if ($(this).hasClass("btn-pendente")) {
            $(this).text("Pendente");
            $("#statusCodes").val("PENDENTE");
        } else if ($(this).hasClass("btn-recebido")) {
            $(this).text("Recebido");
            $("#statusCodes").val("RECEBIDO");
        } else if ($(this).hasClass("btn-cancelado")) {
            $(this).text("Cancelado");
            $("#statusCodes").val("CANCELADO");
        }
    });
});
.btn-pendente {
    width: 140px;
    color: orange !important;
    background-color: transparent;
    border: 2px solid orange !important;
}

.btn-recebido {
    width: 140px;
    color: #28a745;
    background-color: transparent;
    border: 2px solid #28a745;
}

.btn-cancelado {
    width: 140px;
    color: red;
    background-color: transparent;
    border: 2px solid red;
}

这是我的第一个问题,抱歉有什么问题 可以使用切换按钮来在多于两个类之间切换。

英文:

I'm studding Java with Spring boot and Thymeleaf, so i'm creating some views and I've encountered some challenge. I wanted to create a toggle button to "Pendente" and "Recebido", but now, i'm trying to put the option "Cancelado", but I encountered some advertisements.

<div class="row mb-3">
            <label for="statusCodes" class="col-sm-2 col-form-label"
              >Status</label
            >
            <div class="col-sm-2">
              <button
                type="button"
                id="toggleButton"
                class="btn btn-toggle btn-pendente"
                data-value="PENDENTE"
              >
                Pendente
              </button>
              <input
                type="hidden"
                name="statusCodes"
                id="statusCodes"
                value="PENDENTE"
              />
            </div>
          </div>

this is my div, that was working perfectly before.

 $(function () {
    $("#toggleButton").on("click", function () {
      $(this).toggleClass("btn-pendente btn-recebido btn-cancelado");
      if ($(this).hasClass("btn-pendente")) {
        $(this).text("Pendente");
        $("#statusCodes").val("PENDENTE");
      } else if ($(this).hasClass("btn-recebido")) {
        $(this).text("Recebido");
        $("#statusCodes").val("RECEBIDO");
      } else if ($(this).hasClass("btn-cancelado")) {
        $(this).text("Cancelado");
        $("#statusCodes").val("CANCELADO");
      }
    });
  });

I've imagine that will be easy add one more class button, but when I did it, when clicked in "Pendente" button, changes to "Recebido" but with "Cancelado" style

.btn-pendente {
  width: 140px;
  color: orange !important;
  background-color: transparent;
  border: 2px solid orange !important;
}

.btn-recebido {
  width: 140px;
  color: #28a745;
  background-color: transparent;
  border: 2px solid #28a745;
}

.btn-cancelado {
  width: 140px;
  color: red;
  background-color: transparent;
  border: 2px solid red;
}

This is my first question here, sorry for something 可以使用切换按钮来在多于两个类之间切换。

答案1

得分: 1

你可以尝试这样做,应该可以工作

$(function () {
      $("#toggleButton").on("click", function () {
        if ($(this).hasClass("btn-pendente")) {
          $(this).removeClass("btn-pendente")
          $(this).addClass("btn-recebido")
          $(this).text("Recebido");
          $("#statusCodes").val("RECEBIDO");
        } else if ($(this).hasClass("btn-recebido")) {
          $(this).removeClass("btn-recebido")
          $(this).addClass("btn-cancelado")
          $(this).text("Cancelado");
          $("#statusCodes").val("CANCELADO");
        } else if ($(this).hasClass("btn-cancelado")) {
          $(this).removeClass("btn-cancelado")
          $(this).addClass("btn-pendente")
          $(this).text("Pendente");
          $("#statusCodes").val("PENDENTE");
        }
      });
    });
英文:

You can try this, it should work

$(function () {
      $("#toggleButton").on("click", function () {
        if ($(this).hasClass("btn-pendente")) {
          $(this).removeClass("btn-pendente")
          $(this).addClass("btn-recebido")
          $(this).text("Recebido");
          $("#statusCodes").val("RECEBIDO");
        } else if ($(this).hasClass("btn-recebido")) {
          $(this).removeClass("btn-recebido")
          $(this).addClass("btn-cancelado")
          $(this).text("Cancelado");
          $("#statusCodes").val("CANCELADO");
        } else if ($(this).hasClass("btn-cancelado")) {
          $(this).removeClass("btn-cancelado")
          $(this).addClass("btn-pendente")
          $(this).text("Pendente");
          $("#statusCodes").val("PENDENTE");
        }
      });
    });

huangapple
  • 本文由 发表于 2023年3月31日 20:29:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/75898539.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定