簡體   English   中英

jQuery,多個輸入和onBlur

[英]jQuery, multiple inputs and onBlur

所以我有2個輸入。 兩者都是為了“自動完成”。 我也有2個功能幾乎可以完成相同的工作...每個輸入都調用不同的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" />

我想我假設您想為具有相似功能的兩個輸入觸發擊鍵和模糊事件,而這又會影響相同的輸入。

這將是一種方式。 將事件綁定到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" />

編輯:

取消注釋$('body')行以查看正在發生的事件。

希望這就是您想要的。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM