简体   繁体   English

JavaScript中的If语句,用于从Select输入中获取价值

[英]If Statement in JavaScript for Getting Value from Select Input

i get stuck with this problem. 我陷入这个问题。 First, here is the codes : 首先,这里是代码:

JavaScript 的JavaScript

function PilihKeterangan(isi)
{
    if (isi = 'A') {
        $('#keterangan').val("Sangat Baik : sangat aktif mengikuti latihan dan kegiatan ...");
    } else if (isi = "B") {
        $('#keterangan').val("Baik : aktif mengikuti latihan dan kegiatan ...");
    } else if (isi = 'C') {
        $('#keterangan').val("Cukup : cukup aktif mengikuti latihan dan kegiatan Kepramukaan ..."); 
    } else if (isi = 'X') {
        $('#keterangan').val("Kegiatan ini tidak dipilih oleh siswa / bukan menjadi pilihan siswa");
    } else {
        $('#keterangan').val("Pilihlah predikat A/B/C/X.");
    }

} 

HTML 的HTML

<select class="form-control" name="PREDIKAT_EKSTRA_UTS" onchange="PilihKeterangan(this.value)">
    <option selected value="">- Pilih -</option>
    <option value="A">A</option>
    <option value="B">B</option>
    <option value="C">C</option>
    <option value="X">X</option>
</select>

The problem is, it just can select first condition. 问题是,它只能选择第一个条件。 If i click other option, the result is still first conddition. 如果我单击其他选项,则结果仍然是第一个条件。 In example : If i select/click 'A' or 'B' or else one the result is always first condition. 在示例中:如果我选择/单击“ A”或“ B”,否则结果始终是第一个条件。 But if i just passing select's value, it is works but not when i give it IF statement. 但是,如果我只是传递select的值,那是可行的,但是当我给它IF语句时却不能。

I just look from this tutorial about using IF statement in w3schools if the JavaScript is using '=' not the '==' : http://www.w3schools.com/js/js_if_else.asp 我刚刚从本教程看关于W3Schools的使用IF语句,如果JavaScript的使用“=”而不是“==”: http://www.w3schools.com/js/js_if_else.asp

I hope there are any reply. 希望有任何答复。 Thanks :) 谢谢 :)

This is not syntaxt of if statement 这不是if语句的语法

you should do like this : 你应该这样做:

if(isi == 'A'){
    // your code
}

This is called comparison operation : " == " 这称为比较操作:“ ==”

as mentioned by hitesh upadhyay and Oliver you should use equality operator == and not assignment operator = . 如hitesh upadhyay和Oliver所述,您应该使用等于运算符==而不是赋值运算符=

Consider using switch in this case to produce cleaner code. 在这种情况下,请考虑使用switch产生更清晰的代码。

function PilihKeterangan(isi)
{
    var msg = "";
    switch(isi){
        case 'A':
            msg = "Sangat Baik : sangat aktif mengikuti latihan dan kegiatan ...";
        break;
        case 'B':
            msg = "Baik : aktif mengikuti latihan dan kegiatan ...";
        break;
        case 'C':
            msg = "Cukup : cukup aktif mengikuti latihan dan kegiatan Kepramukaan ...");
        break;
        case 'X':
            msg = "Kegiatan ini tidak dipilih oleh siswa / bukan menjadi pilihan siswa";
        break;
        default:
            msg = "Pilihlah predikat A/B/C/X.";
        break;
    }
    $('#keterangan').val(msg);
}

This would be because you are using the assignment operator ( = ) rather than an equality operator ( == ), I would generally suggest using the identity operator ( === ). 这是因为您使用赋值运算符( = )而不是相等运算符( == ),所以我通常建议使用身份运算符( === )。

The if clause must return a boolean value, an assignment does not. if子句必须返回boolean值,而赋值则不会。

The identity ( === ) operator behaves identically to the equality ( == ) operator except no type conversion is done, and the types must be the same to be considered equal. 身份( === )运算符的行为与相等( == )运算符相同,只是不进行类型转换,并且类型必须相同才能被视为相等。

This is covered extensively on this Stack Overflow answer 堆栈溢出答案对此进行了广泛讨论。

您正在使用单个= ,需要使用==进行相等

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM