2012-11-02 52 views
-6

如果選項值和以前的比賽也將在下拉使用jQuery如何檢查下拉選項值與以前的值?

編輯

填充選擇框根據用戶的評論不會顯示

$.each(g_Vehicle, function (index) { 
    var iYear = g_Vehicle[index].Year; 
    sMake = g_Vehicle[index].Make; 
      selectYear = '<option value="' + iYear + '">' + iYear + '</option>'; selectMake = '<option value="' + sMake + '">' + sMake + '</option>'; 
    $("#DropDown_Year").append(selectYear);  
    $("#DropDown_Make").append(selectMake); 
}); 
+0

任何一個可以幫我做這件事 – Anshu

+0

什麼以前的手段? – coolguy

+2

發佈您的代碼和html –

回答

-1
$(document).ready(function(){ 
$('body').delegate('#DropDown_Year','change',function(){ 
var prev = $(this).find('option:selected').prev().val(); 

    if($(this).val() == prev){ 
    alert('Matched'); 
    //your curent value and previous value are the same 
    }else{ 
    //not same 
    } 


}); 
}); 
+0

在我的代碼中,我使用json對象來獲取所有值,而不使用html。它正在工作,但我必須檢查下拉列表中的值。如果這些值已經存在,我將不會在下拉菜單中顯示這是問題 – Anshu

+1

您可以編輯您的問題並向我們展示如何製作下拉菜單嗎? – coolguy

+0

$。每個(g_Vehicle,函數(指數){ VAR iYear = g_Vehicle [指數] .Year; sMake = g_Vehicle [指數]。使; selectYear = '<選項值= 「' + iYear + '」>' + iYear +''; selectMake =''; $(」#DropDown_Year「)。append(selectYear); $(」## DropDown_Make「)。append(selectMake); }); – Anshu

1

您可以將以前是全球性的變量並在變化時訪問它。假定在起始元素中的零位索引被選中。

Live Demo

previousVal = $('#selectId option').eq(0).val();  

$('#selectId').change(function(){ 
    alert(previousVal); 
    previous = $('#selectId').val(); 
});​ 

要存儲您可以用文字()方法,而不是VAL()上一個項目文本

+0

沒有使用alert我怎麼寫這個? – Anshu

+0

你想寫什麼?你可以使用console.log(previousVal) – Adil

相關問題