search

Home  >  Q&A  >  body text

javascript - if statement in change event in jquery fails

<p class="add-new-img mt-2">
    <select name="picType" id="img-type">
        <option value="0">彩色</option>
        <option value="1">黑白</option>
    </select>
</p>
<p class="add-new-img mt-2">
    <select name="picType" id="img-type-classify">
    </select>
</p>

Two drop-down boxes, give the first drop-down box a change event, request a different ajax address, and change the data of the second drop-down box

$("#img-type").change(function(){
        console.log($(this).val())
        if($(this).val()===1){
            console.log(1)
            $.ajax({
            type : 'GET',
            data: {
                'type': '2',
                "type_classify":1
            },
            url: "/Type/getTypeList",
            dataType: "json",
            success: function(data){
                var color = ["彩色","黑白"]

                var options='';
                for(var i=0;i<data.data.length;i++){
                     var num = Number(data.data[i].type_classify)
                    options += '<option value='+data.data[i].id+'>'+data.data[i].id+data.data[i].type_name+"("+color[num]+")"+'</option>';                                     
                }
                $("#img-type-classify").html(options);
                },
              error:function(msg){
                console.log(msg);
            },
        });
        }
        if($(this).val()===0){
            console.log(0)
            $.ajax({
            type : 'GET',
            data: {
                'type': '2',
                "type_classify":0
            },
            url: "/Type/getTypeList",
            dataType: "json",
            success: function(data){
                var color = ["彩色","黑白"]

                var options='';
                for(var i=0;i<data.data.length;i++){
                     var num = Number(data.data[i].type_classify)
                    options += '<option value='+data.data[i].id+'>'+data.data[i].id+data.data[i].type_name+"("+color[num]+")"+'</option>';                                     
                }
                $("#img-type-classify").html(options);
                },
              error:function(msg){
                console.log(msg);
            },
        });
        }
    })

Determine the value of the first drop-down box and request different background interfaces. But the console output $(this).val() is normal, but the console has no effect in the if statement. What is the reason?

巴扎黑巴扎黑2740 days ago642

reply all(2)I'll reply

  • 大家讲道理

    大家讲道理2017-05-19 10:12:24

    if($(this).val()==='1')

    reply
    0
  • 怪我咯

    怪我咯2017-05-19 10:12:24

    0 and 1 are both strings, right? What you used is ===, so...

    reply
    0
  • Cancelreply