【问题标题】:.keyup jquery not working on Iphone.keyup jquery 在 Iphone 上不起作用
【发布时间】:2015-10-28 19:06:09
【问题描述】:

我有点初学者,但我有一个代码网页,它向我的服务器发出一个发布请求并回复一个 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 上不起作用,因为输入元素必须以不同的方式处理。

有没有比我更有知识的人知道更好的解决方案或解决此问题的方法?

【问题讨论】:

  • 在输入变化时使用。保持简单愚蠢:P
  • 我确实想到了,但认为可能有一个聪明的解决方案,Iphones 不能很好地与 jquery 一起工作
  • 这是 jquery mobile 吗?
  • 这里有一些讨论:stackoverflow.com/questions/9940829/… 可能有帮助? (你可能已经在看它了……)

标签: javascript jquery ios iphone


【解决方案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);
        }
    });
}

【讨论】:

    猜你喜欢
    • 2013-10-22
    • 1970-01-01
    • 2012-08-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多