for循环不返回结果

时间:2015-09-05 21:41:37

标签: javascript jquery arrays

我正在创建一个搜索栏,我正在循环一个数组。我正在使用

<input type="text" id="searchInput"/>

在我的搜索栏中显示数组结果。但它返回了数组的最后一个索引。

var inputSearch     = $('#searchInput');
var searchArray     = ['s','e','a','r','c','h'];
var searchReturn    = '';



    for(var i = 0 ; i < searchArray.length ; i++){

        searchReturn = searchArray[i];
        console.log(searchReturn);
        $(inputSearch).attr('placeholder', searchReturn);

    }   

Js

uiObservable
    .switchMap(o -> return anotherUIObservable)
    .subscribeOn(AndroidSchedulers.mainThread())
    .switchMap(o -> return networkObservable)
    .subscribeOn(Schedulers.newThread())
    .observeOn(AndroidSchedulers.mainThread())
    .subscribe(result -> doSomething(result))

2 个答案:

答案 0 :(得分:1)

尝试附加文字,您只需每次分配

for(var i = 0 ; i < searchArray.length ; i++){

    searchReturn = searchReturn + searchArray[i];
    console.log(searchReturn);
    $(inputSearch).attr('placeholder', searchReturn);

}   

答案 1 :(得分:0)

最佳解决方案和最简单的方法是

for(var i = 0 ; i < searchArray.length ; i++){

searchReturn += searchArray[i];
console.log(searchReturn);
$(inputSearch).attr('placeholder', searchReturn);

}