英文:
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?
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论