2012-02-17 54 views
0

我有一個問題,在實現和有效的方式,通過使用JQuery從組合框中選擇和項目,並將其放置在文本字段。 看到下面的圖片時,問題是什麼時候首先,我選擇和元素從1顯示的組合框它正確放置項目所需的文本框顯示2,但後來當我從第二個組合框選擇以前以及當前的變化。整個代碼如下。高效jQuery的方式來選擇項目和地方在文本框

$("input[name^=pasta_text]").focus(function() { 
    var gecerliid=$(this).parent().get(0).id; 
    var mevcutdeger=$(this).val(); 
    var sondeger=$(this).val(); 
    var seciliGrup=""; 
    if(typeof orjinal === "undefined") { 
     var orjinal=$(this); 
    } else { 
     delete orjinal; 
     var orjinal=$(this); 
    } 

    alert($("#pastatext["+gecerliid+"]").id()); 

    if($.inArray(mevcutdeger,Knowledge)>=0) { seciliGrup="Knowledge"; mevcutgrup=0; mevcutsira=$.inArray(mevcutdeger,Knowledge); } 
    if($.inArray(mevcutdeger,Comprehension)>=0) { seciliGrup="Comprehension"; mevcutgrup=1; mevcutsira=$.inArray(mevcutdeger,Comprehension); } 
    if($.inArray(mevcutdeger,Application)>=0) { seciliGrup="Application"; mevcutgrup=2; mevcutsira=$.inArray(mevcutdeger,Application); } 
    if($.inArray(mevcutdeger,Analysis)>=0) { seciliGrup="Analysis"; mevcutgrup=3; mevcutsira=$.inArray(mevcutdeger,Analysis); } 
    if($.inArray(mevcutdeger,Synthesis)>=0) { seciliGrup="Synthesis"; mevcutgrup=4; mevcutsira=$.inArray(mevcutdeger,Synthesis); } 
    if($.inArray(mevcutdeger,Evaluation)>=0) { seciliGrup="Evaluation"; mevcutgrup=5; mevcutsira=$.inArray(mevcutdeger,Evaluation); } 

    //$("#pasta_div").text(seciliGrup).show(); 
    $(this).hide(); 
    $("#learn_outcome").attr("size","49"); 
    $('input[name=pasta_submit], select[name=pasta_grup], select[name=pasta_fiil]').remove(); 

    $(this).before("<select name='pasta_grup' class='formin'>"); 
    //<select name='pasta_grup[".($s+1)."]' class='formin'><option value='-1'>GRUP</option></select><select name='pasta_fiil[".($s+1)."]' class='formin'><option value='-1'>FİİL</option></select><input type='button' value='OK' class='formbutton' name='pasta_submit' id='pasta_submit'> 
    $.each(taxonomy, function(key, value) { 
     $("select[name=pasta_grup]") 
       .append($('<option>', { value : key }) 
       .text(value)); 
    }); 
    $(this).before("</select>"); 
    $("select[name=pasta_grup]").val(mevcutgrup); 

    $("select[name=pasta_fiil]").remove(); 
    $(this).before("<select name='pasta_fiil' class='formin'>"); 
    fiilOlustur(mevcutgrup);   
    $("select[name=pasta_fiil]").val(mevcutsira).focus(); 
    $(this).before("</select>"); 
    $(this).before("<input type='button' value='OK' class='formbutton' name='pasta_submit'>"); 

    $("#pasta_div").text("mevcutdeger:"+mevcutdeger+" mevcutsira:"+mevcutsira+" mevcutgrup:"+mevcutgrup+" eleman:"+$(this).id); 

    $("select[name=pasta_grup]").live("change",function() { 
     mevcutgrup=$("select[name=pasta_grup] option:selected").val(); 
     $('select[name=pasta_fiil]').empty(); 
     fiilOlustur(mevcutgrup); 
     $('select[name=pasta_fiil] option:first').attr("selected","selected"); 
    }); 

    $("select[name=pasta_fiil]").live("change",function() { 
     sondeger=$("select[name=pasta_fiil] option:selected").text(); 
     orjinal.val(sondeger); 
     $("#pasta_div").text("post change: "+sondeger); 
    }); 

    $("input[name=pasta_submit]").live("click", function() { 
     orjinal.show().empty(); 
     sondeger = $("select[name=pasta_fiil] option:selected").text(); 
     $("#pasta_div").text("post click: "+sondeger); 
     orjinal.addClass("forminred").blur(function() { $(this).show(); }); 
     $('input[name=pasta_submit], select[name=pasta_grup], select[name=pasta_fiil]').remove(); 
    }); 

    $("#pasta_div").text("last level: "+sondeger); 
    //$("#pasta_div").text("sondeger: "+sondeger+" sonra: "+$(this).val()); 
    $(this).val(sondeger).addClass("forminred").blur(function() { $(this).show(); }); 

    function fiilOlustur(grupno) { 
     if(grupno==0) { $.each(Knowledge, function(key, value) { $('select[name=pasta_fiil]').append($('<option>', { value : key }).text(value)); }); } 
     if(grupno==1) { $.each(Comprehension, function(key, value) { $('select[name=pasta_fiil]').append($('<option>', { value : key }).text(value)); }); } 
     if(grupno==2) { $.each(Application, function(key, value) { $('select[name=pasta_fiil]').append($('<option>', { value : key }).text(value)); }); } 
     if(grupno==3) { $.each(Analysis, function(key, value) { $('select[name=pasta_fiil]').append($('<option>', { value : key }).text(value)); }); } 
     if(grupno==4) { $.each(Synthesis, function(key, value) { $('select[name=pasta_fiil]').append($('<option>', { value : key }).text(value)); }); } 
     if(grupno==5) { $.each(Evaluation, function(key, value) { $('select[name=pasta_fiil]').append($('<option>', { value : key }).text(value)); }); } 
    } 

    /*if(seciliGrup<>"") { 
     fiilFonksiyon(); 
    }*/ 

    /*var mevcutdeger=$(this).index(); 
    $("#pasta_div").text(mevcutdeger).show(); 
    grupFonksiyon();*/ 
}); 

您能指導我如何使用JQuery以更高效的方式實現它嗎? 有沒有其他方式可以解決我的問題? 謝謝。

enter image description here

回答

0

所以,你必須具有相同名稱的選擇框,是你的代碼會做肯定只有一個選擇框的值和值寫入文本框。

,因爲你硬編碼的選擇框名

sondeger = $("select[name=pasta_fiil] option:selected").text(); 

使用「this」關鍵字來從選擇列表框的值。

sondeger = $(this).val(); // this will return you value from selected listbox, 
+0

謝謝您的回覆。我發佈了我的整個代碼。有沒有其他更簡單的方法通過使用JQuery以更高效和更簡單的方式實現我的問題,因爲我堅持了兩天,並且看不到我的代碼。 – Shahriar 2012-02-17 10:51:03