变量在if语句中没有正确更新 - Javascript / Jquery

时间:2017-11-08 07:55:12

标签: javascript jquery

我正致力于为电动工具评级的工具。我尝试这样做,以便在更新select元素时,它会检查当前选项的值并禁用并启用它周围的某些元素。 我遇到的问题是我使用的if语句没有更新值并且卡在默认选项上。

我正在使用的代码:

HTML

<select class='form-control garDropdown' id='garDropdown' name='gar'>
<option value='goedgekeurd'>Goedgekeurd</option>
<option value='reparatie'>Na reparatie goedgekeurd</option>
<option value='afgekeurd'>Afgekeurd</option></select>

JS

$(document).on('change', '.garDropdown', function(){
var e = document.getElementById("garDropdown");
var y = e.value;
    console.log(y);
    if (y = "goedgekeurd"){
      $('.repverText').prop('disabled', 'disabled');
      $('.afkeurText').prop('disabled', 'disabled');
      $('.toelichtText').prop('disabled', 'disabled');
      $('.ipwText').prop('disabled', 'disabled');
      console.log(y)
    } else if (y = "reparatie") {
      $('.repverText').prop('disabled', 'disabled');
      $('.afkeurText').prop('disabled', 'disabled');
      $('.toelichtText').prop('disabled', false);
      $('.ipwText').prop('disabled', 'disabled');
      console.log(y)
    } else if (y = "afgekeurd"){
      $('.repverText').prop('disabled', false);
      $('.afkeurText').prop('disabled', false);
      $('.toelichtText').prop('disabled', false);
      $('.ipwText').prop('disabled', 'disabled');
      console.log(y)
}
});

初始console.log(y)打印正确的值,但语句中的console.log(y)不断打印&#39; goedgekeurd&#39;。我看到的是什么,这种格式甚至是可能的,还是我首先完全解决了这个问题?

谢谢!

编辑:忘了一行JS代码

3 个答案:

答案 0 :(得分:1)

您目前正在测试y = "goedgekeurd"的赋值是否为真,它始终是真的,此赋值永远不会失败,因此您将始终触发第一个if语句。

您需要使用与赋值运算符相对的等于运算符,因此: if( y == "goedgekeurd" )会检查您传递的值是否相同。或者,您可以使用===检查您要比较的对象的值和类型,例如1 == "1"为true,但1 === "1"为false,因为左侧是in并且右手边是一根绳子。

解决方案:

$(document).on('change', '.garDropdown', function(){
var e = document.getElementById("garDropdown");
var y = e.value;
console.log(y);
if (y === "goedgekeurd"){
    $('.repverText').prop('disabled', 'disabled');
    $('.afkeurText').prop('disabled', 'disabled');
    $('.toelichtText').prop('disabled', 'disabled');
    $('.ipwText').prop('disabled', 'disabled');
    console.log(y)
} else if (y === "reparatie") {
    $('.repverText').prop('disabled', 'disabled');
    $('.afkeurText').prop('disabled', 'disabled');
    $('.toelichtText').prop('disabled', false);
    $('.ipwText').prop('disabled', 'disabled');
    console.log(y)
} else if (y === "afgekeurd"){
    $('.repverText').prop('disabled', false);
    $('.afkeurText').prop('disabled', false);
    $('.toelichtText').prop('disabled', false);
    $('.ipwText').prop('disabled', 'disabled');
    console.log(y)
}
});

答案 1 :(得分:0)

$(document).on('change', '.garDropdown', function(){
var e = document.getElementById("garDropdown");
var y = e.value;
    console.log(y);
    if (y === "goedgekeurd"){
      $('.repverText').prop('disabled', 'disabled');
      $('.afkeurText').prop('disabled', 'disabled');
      $('.toelichtText').prop('disabled', 'disabled');
      $('.ipwText').prop('disabled', 'disabled');
      console.log(y)
    } else if (y === "reparatie") {
      $('.repverText').prop('disabled', 'disabled');
      $('.afkeurText').prop('disabled', 'disabled');
      $('.toelichtText').prop('disabled', false);
      $('.ipwText').prop('disabled', 'disabled');
      console.log(y)
    } else if (y === "afgekeurd"){
      $('.repverText').prop('disabled', false);
      $('.afkeurText').prop('disabled', false);
      $('.toelichtText').prop('disabled', false);
      $('.ipwText').prop('disabled', 'disabled');
      console.log(y)
 }
});

答案 2 :(得分:0)

if语句的实现是错误的,这是一个赋值语句,并且将始终保持为true。始终执行if语句

如果声明应该是那样

if (y == "goedgekeurd"){
//Do something here
}
else if (y == "reparatie") {
//Do something here
}
else if (y == "afgekeurd"){
//Do something here
}