jQuery if / else语句失败

时间:2011-10-12 02:27:47

标签: jquery if-statement

我有这段代码,在更改时在选择框中查找值,并使用jQuery .text设置html元素,以下代码中的第一个if语句似乎有用,“Green”但是“Blue” “如果没有,任何想法为什么?

$("#firstdrop").change(function() { 
    if ($("#firstdrop").val() == "Green") { 
        $('.price-tag').text('Green $5.00');
    } 
    if ($("#firstdrop").val() == "Blue") { 
        $('.price-tag').text('Blue $15.00');
    }        
});

帮助表示赞赏。

由于

1 个答案:

答案 0 :(得分:0)

我为你做了一个example来从select中获取所选的值。

<select id="firstdrop">
    <option value="-">-Select-</option>
    <option value="Green">Green</option>
    <option value="Blue" >Blue</option>
</select>

$('#firstdrop').change(function() { 
         var sel_value = $("#firstdrop option:selected").val(); 
         if (sel_value == 'Green') { 
            $('.price-tag').text('Green $5.00');
         } 
         if (sel_value == 'Blue') { 
            $('.price-tag').text('Blue $15.00');
         }
         if( sel_value == '-')
         {
            $('.price-tag').text('0');
         } 
});