【问题标题】:Unable to write over selected text on a input无法在输入上覆盖选定的文本
【发布时间】:2023-03-05 05:39:01
【问题描述】:

我无法在 Android 2.2.1 的输入上覆盖选定的文本。

以下example 选择输入文本,但maxlength 属性阻止写入新值。

如果我删除 maxlength,新值将连接在前一个值之后。

编辑:如果我使用 click 事件而不是 keyup example 工作正常。

【问题讨论】:

    标签: javascript android jquery html mobile


    【解决方案1】:

    试试这个

    $( '#current1' ).keyup( function(e) {
        $( '#current2' ).focus();
        $( '#current2' ).selectRange( 0, 1 );
    });
    
    $.fn.selectRange = function(start, end) {
        var e = document.getElementById($(this).attr('id')); // I don't know why... but $(this) don't want to work today :-/
        if (!e) return;
        else if (e.setSelectionRange) { e.focus(); e.setSelectionRange(start, end); } /* WebKit */
        else if (e.createTextRange) { var range = e.createTextRange(); range.collapse(true); range.moveEnd('character', end); range.moveStart('character', start); range.select(); } /* IE */
        else if (e.selectionStart) { e.selectionStart = start; e.selectionEnd = end; }
    };
    

    【讨论】:

    • 发生相同的行为,它选择文本但阻止在其上书写。
    • 您用过哪个版本的Android?该代码在除 Android 2 之外的大多数移动操作系统上都能正常工作。*
    • 此问题仅在 Android 版本 2 上发生。*
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-28
    • 2013-06-14
    • 1970-01-01
    • 2013-09-26
    • 1970-01-01
    • 2017-06-20
    相关资源
    最近更新 更多