lundi 1 juillet 2019

Adding to conditions to determine if value should increase or not

I'm looking to create conditions for my ticket system with jQuery. I have my code that I can increase or decrease the basket total depending on the number of tickets but I want to take a step further and have conditions that have to be met to add the tickets with pop ups that tell you there's an issue

For example: you can't have a child ticket without and adult ticket... or you can't have more than 2 carers... etc

I've tried adding additional if statements within my code that adds increments/decrements the ticket totals but I can't get the conditions to work

I tried using a switch statement but I'm really not that experienced with JS to execute that properly but I feel it may be an option?

Here's a simplified snippet of my code:

  var totalPrice = 0;

  $($(".valueBtn")).on("click", function(e) {
  var currTicket = $(this).closest("li");
  var targetClass = e.target.className;
  var targetTicketType = $(this)
    .closest("li")
    .attr("data-type");
  var targetTicketPrice = $(this)
    .closest("li")
    .attr("data-price");

  //plus value
  if (targetClass.indexOf("plus") > 0) {
    var currAmount = $(currTicket)
      .find(".currentValue")
      .html();
    if (currAmount >= 0) {
      $(currTicket)
        .find(".currentValue")
        .html(
          parseInt(
            $(currTicket)
              .find(".currentValue")
              .html()
          ) + 1
        );
      totalPrice = totalPrice + parseFloat(targetTicketPrice);
    }
  }

  //minus value
  if (targetClass.indexOf("minus") > 0) {
    var currAmount = $(currTicket)
      .find(".currentValue")
      .html();
    if (currAmount > 0) {
      $(currTicket)
        .find(".currentValue")
        .html(
          parseInt(
            $(currTicket)
              .find(".currentValue")
              .html()
          ) - 1
        );
      totalPrice = totalPrice - parseFloat(targetTicketPrice);
    }
  }

  //update basket total
  $("#basket").html(parseFloat(totalPrice).toFixed(2));
});

You can see it here: https://codepen.io/cheesycoder/pen/KLjWPM

I'm just looking for a nudge in the right direction.

If anyone can help me create conditions that won't allow child tickets to be added without adult tickets maybe I can work out the rest of my conditions from there.

Aucun commentaire:

Enregistrer un commentaire