javascript - if statement in change event in jquery fails
巴扎黑
巴扎黑 2017-05-19 10:10:24
0
2
644
<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?

巴扎黑
巴扎黑

reply all(2)
大家讲道理

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

刘奇

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

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template