2010-02-05 107 views
0

所以我有2個輸入。兩者都是爲了「自動完成」。我也有兩個函數可以做幾乎相同的事情......每個輸入在onBlur上調用一個不同的函數。問題在於第二次爲第二次輸入選擇「建議」時,它填充第一次。我能做些什麼來讓第二個人填充第二個?jQuery,多個輸入和onBlur

有可能是一個更好的方式做到這一點,但我的jQuery/JavaScript的知識是有限的,我沒時間了......謝謝!

代碼:

function fillLocation(thisValue) { 
    $('#location-box').val(thisValue); 
    setTimeout("$('#suggestionsLocation').fadeOut('fast');", 200); 
} 

function fillTerms(thisValue) { 
    $('#terms-box').val(thisValue); 
    setTimeout("$('#suggestionsTerms').fadeOut('fast');", 200); 
} 


<input type="text" name="search_location" id="location-box" value="" onkeyup="suggestLocation(this.value);" onblur="fillLocation();" autocomplete="off" /> 

<input type="text" name="search_terms" id="terms-box" value="" onkeyup="suggestTerms(this.value);" onblur="fillTerms();" autocomplete="off" /> 
+0

我很困惑,因爲你有fillTerms這需要一個參數,但隨後你的onblur您呼叫fillTerms()不帶參數 – 2010-02-05 23:31:55

+0

而你指的是你沒有提供代碼的元素('#terms-box')。你能發佈一個更完整的代碼示例嗎?也許可以引用元素的ID名稱而不是「第一/第二輸入」。這將澄清它(至少對我而言))。 – 2010-02-05 23:33:08

+0

@Felix - 我認爲他的意思是回頭看輸入,因爲ID被分配給運行這些功能的相同輸入。 – user113716 2010-02-05 23:41:24

回答

2

我猜我假設你想要觸發KEYUP和模糊事件的兩個輸入端具有類似的功能,並且都將反過來影響相同的輸入。

這將是一種方式。綁定的事件在JavaScript,不是HTML,並運行正確的代碼

$('document').ready(function() { 

    $('#location-box,#terms-box') 
        .bind('keyup', function() { 
          // do your keyup thing using $(this) 
          // to refer back to the input 
          //$('body').append($(this).attr('id') + ': keyup<br />'); 
          var theValue = $(this).val(); 
        }) 
        .bind('blur', function() { 
          // do your blur thing using $(this) 
          // to refer back to the input 
          //$('body').append($(this).attr('id') + ': blur<br />'); 
        }) 
    }); 


<input type="text" name="search_location" id="location-box" value="" autocomplete="off" /> 
<input type="text" name="search_terms" id="terms-box" value="" autocomplete="off" /> 

編輯:

取消對$(「身體」)線在行動中看到的事件。

希望這是你正在尋找。

編輯:添加代碼到keyup事件檢索值

+0

這幫了一把!謝謝! – mike 2010-02-08 16:57:08