javascript - jQuery 更改事件中忽略的条件?

标签 javascript jquery html css conditional-statements

因此,我尝试根据输入启用/禁用按钮,但由于某种原因,我不知道为什么,它不会按照我想要的方式执行。

  • 如果输入的值等于或介于 1-10 之间:启用按钮。
  • 如果输入值小于 1:禁用按钮。

在更改输入值时,无论值如何,按钮始终处于启用状态。如果值设置为 0,则不会禁用它。 变量 inputIn 保存输入,变量 inputBtn 保存按钮,因此您知道在哪里查看。

如果您发现我还有其他需要修复的地方,请告诉我,因为我对 jQuery 和 Javascript 还很陌生。

更新:

通过将更改事件中的所有语句更改为 if 语句来解决问题。但不是最佳的?

$(document).ready(function() {
let $table = $(".shoe-table");

fetch("shoes.json")
.then(resp => resp.json())
.then(data => {
  let shoes = data.shoes;
  let rows = [1, 2, 3];
  let shoeCard = "";

  let counter = 0;
  rows.forEach(row => {
    shoeCard += "<tr>";
    for (let index = 0; index < 4; index++) {
      shoeCard +=
        "<td class='card text-center'>" +
        "<img class='card-img-top' src=" +
        shoes[counter].image +
        " alt='Card image cap'>" +
        "<h5 class='card-title'>" +
        shoes[counter].name +
        "</h5>" +
        "<p class='card-text'>kr " +
        shoes[counter].price +
        "</p>" +
        "<button id=" +
        counter +
        " class='orderNow btn btn-outline-dark'>ORDER NOW</button>" +
        "<div id=" + counter + " class='input-form'><input class='qty-chooser' type='number' placeholder='Choose quantity' min=0 max=10>" +
        "<button class='btn-add-to-cart' disabled='disabled'>ADD TO CART</button>" +
        "</div>" +
        "</td>";
      counter++;
    }

    shoeCard += "</tr>";
  });

  $table.append(shoeCard);
  let $inputForm = $(".input-form");
  let $orderBtn = $(".orderNow");

  $inputForm.hide();

  $orderBtn.click(toggleOrder);

  function toggleOrder() {
    let clickedBtn = $(this);
    let thisInputForm = $("#" + clickedBtn.attr("id") + ".input-form");


    let inputBtn = thisInputForm.children("button");
    let inputIn = thisInputForm.children("input");

    function resetInputForm() {
      inputBtn.css("background", "");
      inputBtn.css("color", "");
      inputBtn.attr("disabled", "disabled");
      inputIn.val(null);
    }

    inputBtn.click(function() {
      console.log("ADDING");
      thisInputForm.hide("slow");
      clickedBtn.text("ORDER NOW");
      clickedBtn.css("background", "");
      resetInputForm();
    });
     // The change function with conditions that do not work
    inputIn.change(function() {
      console.log("change");
      let qty = inputIn.val();
      if (qty >= 1 || qty <= 10) {
        inputBtn.removeAttr("disabled");
        inputBtn.css("background", "rgb(15, 163, 10)");
        inputBtn.css("color", "white");
      } else if (qty < 1) {
        resetInputForm();
      }
    });

    if (clickedBtn.text() == "CANCEL") {
      thisInputForm.hide("slow");
      clickedBtn.text("ORDER NOW");
      clickedBtn.css("background", "");
      resetInputForm();
    } else {
      thisInputForm.show("slow");
      clickedBtn.text("CANCEL");
      clickedBtn.css("background", "red");
      inputBtn.attr("disabled", "disabled");
      resetInputForm();
    }
  }
})
.catch(err => console.error(err));
});

最佳答案

在您的代码中发现两个错误

    html实体的
  1. id应该是唯一的。您正在向 Div 提供相同的 id 和 ordernow 按钮。
  2. 如果发生变化,您的情况应阅读此 qty >= 1 && qty <= 10因为你的值应该在 1 到 10 之间。

关于javascript - jQuery 更改事件中忽略的条件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60576755/

相关文章:

html - 使用关键帧和 div 的动画背景 - 使其充满屏幕滚动(html 和 CSS)

HTML CSS 包含

javascript - 使用 cryptojs 使用 SHA256 算法生成符合 RFC 2104 的 HMAC

javascript - Angular $q在http请求之前执行 "then"

javascript - 新的对象数组,但键已更改

javascript - 从屏幕左侧滑动到内容右侧

html - 如何为 Junit 结果生成 HTML 报告?

javascript - 在响应式(Reactive)表单中使用 formArray 时获取未定义的 "TypeError: Cannot read property ' 控件

javascript - 带有引号的 JSON 对象键 - 如何实现?

javascript - 无法附加选项以进行选择