保持iOS键盘打开,而keyup功能运行

Keep iOS keyboard open while keyup function runs

本文关键字:keyup 功能 运行 iOS 键盘 保持      更新时间:2023-09-26

这个问题是关于iOS web开发的。

我有一个函数,它是由一个键up监听器在"搜索"输入字段。它在台式电脑、笔记本电脑、三星(Samsung) Galaxy手机和索尼(Sony) Xperia上运行完美,但不适用于iPhone或iPad。当函数运行时,软键盘关闭。我想到的一个潜在的修复是将输入id='newsearch'上的focus作为函数的最后一行,但这似乎没有做任何事情。

有谁知道如何保持键盘打开,而功能在ios中运行?

见下面3个代码片段:

触发函数的行:

<tr><td colspan='3'>Search <input type='text' autofocus id='newsearch'></input></td>
    <h2>Search Results</h2>
    <div id="newsearch-data"> </div>
<script src="http://code.jquery.com/jquery-1.8.0.min.js"> </script>
<script src="addplayer.js"> </script>

函数本身 addplayer.js

$('input#newsearch').on('keyup', function(){
    var newsearch = $('input#newsearch').val();
    if ($.trim(newsearch)!= "") {
        $.post('newsearch.php', {newsearch: newsearch}, function(data) {
            $('div#newsearch-data').html(data);
        });
    }
});
            document.getElementById("newsearch").focus();

函数指向的php文件: newsearch.php

if (isset($_POST['newsearch']) && $_POST['newsearch'] != NULL){
    require 'dbconnect.php';
    $term = $_POST['newsearch'];
    $terms = "%" . $term . "%";
    $_SESSION['players'] = array();
    $query = ("SELECT * FROM players WHERE Username LIKE '$terms' OR Phonenumber LIKE '$terms' OR PlayerID LIKE '$terms'");
    $run_query = mysqli_query($dbcon, $query);
        echo "<table border='1px'><tr><th>Player ID</th><th>Name</th><th>Action</th></tr>";
            while ($dbsearch = mysqli_fetch_assoc($run_query))
             {
                $dbu = $dbsearch['Username'];
                $id = $dbsearch['PlayerID'];
                $func = "add(" . $id . ", Brad Henry )";
             array_push ($_SESSION['players'],$dbsearch['PlayerID']);
             echo "<tr><td>".$id ."</td><td>".$dbu."</td><td><input type='"submit'" id='"PlayerAdded".$id."'" autofocus value='"Add'" onclick='"add('".$id."','".$dbu."');'"></input></td></tr>";
             }

尝试将焦点设置在字段上。

$('input#newsearch').on('keyup', function(){
    var field = $(this);
    var newsearch = field.val();
    if ($.trim(newsearch)!= "") {
        $.post('newsearch.php', {newsearch: newsearch}, function(data) {
            $('div#newsearch-data').html(data);
            field.focus();
        });
    }
});

我真不敢相信我花了这么长时间才意识到这一点。最后的'echo'在newsearch.php中包含添加按钮....上的autofocus属性我删除了它,现在它可以工作了