How to store and compare current and previous category IDs in JavaScript and nullify subcategory if they differ

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

How to store and compare current and previous category IDs in JavaScript and nullify subcategory if they differ

问题

我想要将当前类别的ID存储在一个变量中,如果类别发生变化,也要存储新的类别ID,并想要比较这两个ID,如果它们不相等,就将子类别设为null,我不知道我哪里错了。

我尝试使用JavaScript,但两个ID都相同。

$(document).ready(function () {
  myFunction();
  function myFunction() {
    var previousCategory = $("#selectcategory").val();
    var category_id = previousCategory;
    var category_id = $("#selectcategory").val();
    var subcat_id = $("#selectsubcat").val();
    console.log(previousCategory, category_id);
    if (category_id !== previousCategory) {
      $("#selectsubcat").val(null);
      subcat_id = null;
    }
    // console.log(category_id, subcat_id);
  }
  $("#search").keyup(myFunction);
  $("#selectsubcat").on("change", myFunction);
  $("#selectcategory").on("change", myFunction);
});
英文:

I want to set store current category id in one variable, and if category changes then store that also and wants to compare that both id, if both is not equal then set sub cat=null, I don't know where am I doing wrong

I have tried using using JavaScript , but both ids are coming same

$(document).ready(function () {
  myFunction();
  function myFunction() {
    var previousCategory = $("#selectcategory").val();
    var category_id = previousCategory;
    var category_id = $("#selectcategory").val();
    var subcat_id = $("#selectsubcat").val();
    console.log(previousCategory, category_id);
    if (category_id !== previousCategory) {
      $("#selectsubcat").val(null);
      subcat_id = null;
    }
    // console.log(category_id, subcat_id);
  }
  $("#search").keyup(myFunction);
  $("#selectsubcat").on("change", myFunction);
  $("#selectcategory").on("change", myFunction);
});

答案1

得分: 1

在你的代码中,'previousCategory' 和 'category_id' 获得相同的值。例如,previousCategory='5',category_id='5'。如果条件判断两个 ID 不相同,那么它应该将 'selectsubcat' 设置为 null,对吗?

英文:

In your code 'previousCategory' and 'category_id' get same value. For example previousCategory='5' and category_id = '5'.
If condition says if both the id's are not same then it should set null in 'selectsubcat'. Right?

huangapple
  • 本文由 发表于 2023年6月1日 19:43:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/76381522.html
匿名

发表评论

匿名网友

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

确定