如果jquery DropDown选中的值是“y / n”更新文本字段

时间:2011-12-21 20:06:10

标签: jquery-selectors if-statement

当下拉列表选择为“是”或“否”时,我想要更新文本框。我检查的代码是Y或N的选项。如何检查选择了哪个值?

HTML

<select id="SelectionFlag" name="SelectionFlag">
<option value="">Select one</option>
<option selected="selected" value="Y">Yes</option>
<option value="N">No</option>
</select>

jquery的

 $("#SelectionFlag").change(function () {
                var currentYear = (new Date).getFullYear();
                alert(currentYear + 1);
                if ($("#SelectionFlag").value = "N") {

                    $("#TermDate").val("12/31/" + currentYear);
                }
                if($("#SelectionFlag").value = "Y")
                {
                    $("#EffectiveDate").val("12/31/" + (currentYear + 1));
                }
            });

3 个答案:

答案 0 :(得分:4)

将您的IF语句更改为:

if ($("#SelectionFlag").val() == "N")

if ($("#SelectionFlag").val() == "Y")

答案 1 :(得分:1)

$("#SelectionFlag").change(function() {
    var currentYear = (new Date).getFullYear();
    // alert(currentYear + 1);
    if ($(this).val() == "N") {
        $("#TermDate").val("12/31/" + currentYear);
    } else if ($(this).val() == "Y") {
        $("#EffectiveDate").val("12/31/" + (currentYear + 1));
    }
});

http://jsfiddle.net/mblase75/n4Ggz/

答案 2 :(得分:0)

$("#SelectionFlag").value替换为$("#SelectionFlag").val()