gpt4 book ai didi

javascript - if 语句发生变化

转载 作者:行者123 更新时间:2023-11-30 14:08:09 25 4
gpt4 key购买 nike

我想检查的动态值和变量:

jQuery("select.super-attribute-select").change(function() {
//console.log("New Product ID: " + this.value);
var option_id11 = "Yes";
var option_id12 = "No";
var selectedvalue = this.value;

//var optionvalue = "option_id"+selectedvalue;

if ("option_id" + selectedvalue === "Yes") {
console.log('hide');
} else if ("option_id" + selectedvalue === "No") {
console.log('show');
} else {
console.log('Nothing');
}

});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<select name="super_attribute[161]" data-selector="super_attribute[161]" data-validate="{required:true}" id="attribute161" class="super-attribute-select" aria-required="true">
<option value="">Choose an Option...</option>
<option value="11">REFURB</option>
<option value="12">NEW</option>
</select>

但它总是在控制台中显示Nothing。请帮忙

最佳答案

您不能使用表示变量名称的字符串来调用变量。尝试创建一个对象来指定值:

jQuery("select.super-attribute-select").change(function() {
//console.log("New Product ID: " + this.value);

var options = {
option_id11: "Yes",
option_id12: "No"
}

var selectedvalue = this.value;

var selectedOptionValue = options["option_id"+selectedvalue];

if (selectedOptionValue === "Yes") {
console.log('hide');
} else if (selectedOptionValue === "No") {
console.log('show');
} else {
console.log('Nothing');
}

});

关于javascript - if 语句发生变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54983099/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com