2013-04-02 25 views
0

我目前正在修改一個開箱即用的php/jQuery自我暗示腳本。我很努力讓腳本能夠處理多個文本輸入字段。 它最初與ONE字段一起工作。在jQuery中獲得專注的表單字段ID

這裏是ORIGINALE腳本和表單代碼:

function suggest(inputString){ 
    if(inputString.length == 0) { 
     $('#suggestions').fadeOut(); 
    } else { 
    $('#city').addClass('load'); 
     $.post("autosuggest.php", {queryString: ""+inputString+""}, function(data){ 
      if(data.length >0) { 
       $('#suggestions').fadeIn(); 
       $('#suggestionsList').html(data); 
       $('#city').removeClass('load'); 
      } 
     }); 
    } 
} 

function fill(thisValue) { 
    $('#city').val(thisValue); 
    setTimeout("$('#suggestions').fadeOut();", 0); 
} 

修改腳本有多個輸入字段的工作方式,我相信,會得到集中的形式元素的ID,然後做一些事情像這樣:

function getFocusID() { 
    // get the focused form field id here 
} 

if(focusedID == "city") { 
    //Script for form field with "city" as id 

    function suggest(inputString){ 
     if(inputString.length == 0) { 
      $('#suggestions').fadeOut(); 
     } else { 
     $('#city').addClass('load'); 
      $.post("autosuggest.php", {queryString: ""+inputString+""}, function(data){ 
       if(data.length >0) { 
        $('#suggestions').fadeIn(); 
        $('#suggestionsList').html(data); 
        $('#city').removeClass('load'); 
       } 
      }); 
     } 
    } 

    function fill(thisValue) { 
     $('#city').val(thisValue); 
     setTimeout("$('#suggestions').fadeOut();", 0); 
    } 
} // code for input id "city" END 

// And now create if else statements for the other 4 input text fields, filling them with the code above. 

我在這裏認爲正確嗎?或者還有其他更有效的方法來做到這一點? 如果這是正確的方法,我如何獲得焦點輸入文本字段的inpud ID?

回答