.keyup jquery没有在Iphone上工作

时间:2015-10-28 19:06:10

标签: javascript jquery ios iphone

我有点像初学者,但我有一个代码网页,它向我的服务器发出一个帖子请求,并使用JSON编码的关联数组数组进行回复。

接下来我使用文本输入字段,在.keyup上,代码可以作为一种搜索功能使用数组。

$( function() {
$('#questionSearchInput').keyup(function(){
    var list = [];
    search = $(this).val().toString();
    searchArray = search.split(" ");
    console.log(searchArray);
    $.each(grabbedItems, function(j, question){
        question.score = 0;
        console.log(question.score);
    });
    if(searchArray[0]){
        $.each(grabbedItems, function(i, question){
            lowerName = question.name.toLowerCase();
            if(lowerName.indexOf(searchArray[0]) > -1){
                question.score = question.score + 1;
                console.log(question.score);
            }
        });
    }

它为搜索数组中的每个元素运行if(searchArray [1])部分几次,然后动态加载到页面上。

        grabbedItems.sort(function(a, b){
        return b.score - a.score;
    });
    if(grabbedItems.length < 1){
        document.getElementById("noQuestionsP").style.display = "block";
    }
    clearBox("questionNamesDisplay");
    $.each(grabbedItems, function(k, question){
        if(k < 5){
            var place = document.getElementById("questionNamesDisplay");
            var item = document.createElement("a");
            item.innerHTML = question.name;
            item.href = question.link;
            item.style.display = "block";
            place.appendChild(item);
        }
    });
});

});

我的意思是.keyup不能在Iphone上工作,因为必须以不同的方式处理输入元素。

知识比我更多的人知道更好的解决方案或解决方法吗?

1 个答案:

答案 0 :(得分:1)

从设备中删除触摸点时会触发touchend事件。

https://developer.mozilla.org/en-US/docs/Web/Events/touchend

您可以将keyuptouchend个事件传递到.on() jQuery方法(而不是keyup()方法),以触发这两个事件的代码。

$('#questionSearchInput').on('keyup touchend', function(){
var list = [];
search = $(this).val().toString();
searchArray = search.split(" ");
console.log(searchArray);
$.each(grabbedItems, function(j, question){
    question.score = 0;
    console.log(question.score);
});
if(searchArray[0]){
    $.each(grabbedItems, function(i, question){
        lowerName = question.name.toLowerCase();
        if(lowerName.indexOf(searchArray[0]) > -1){
            question.score = question.score + 1;
            console.log(question.score);
        }
    });
}