简体   繁体   English

JavaScript插入位置

[英]JavaScript caret position

I'm developing a mobile banking application and I'm interested in formatting the currency amount inputs in real time. 我正在开发一个移动银行应用程序,我有兴趣实时格式化货币金额输入。

I've already tested autoNumeric plugin and jQuery Format Currency Plugin but both have cursor position issues on Android 2.* browsers. 我已经测试了autoNumeric插件jQuery格式货币插件,但两者都在Android 2. *浏览器上有光标位置问题。

Does anyone have a JavaScript solution compatible with this browsers? 有没有人有与这个浏览器兼容的JavaScript解决方案?

I don't know about autoNumeric, but http://code.google.com/p/jquery-formatcurrency/ looks pretty good. 我不知道autoNumeric,但http://code.google.com/p/jquery-formatcurrency/看起来很不错。 The jsFiddle example you posted doesn't place the caret correctly on my desktop browser, but this does, and on my (Jellybean) Android phone as well. 您发布的jsFiddle示例并未将插入符号正确放置在我的桌面浏览器上,但这样做,以及我的(Jellybean)Android手机上也是如此。 (On Gingerbread, the blinking cursor bar may jump to the end of the line, but you will still be editing where it last was.) Try their demo: http://www.bendewey.com/code/formatcurrency/demo/format_as_you_type.html (在Gingerbread上,闪烁的光标栏可能会跳到行尾,但您仍然会在最后的位置进行编辑。)尝试他们的演示: http//www.bendewey.com/code/formatcurrency/demo/format_as_you_type html的

I usually use accounting.js You can download it from http://openexchangerates.github.io/accounting.js/#download 我通常使用accounting.js你可以从http://openexchangerates.github.io/accounting.js/#download下载它

It's quite easy to use. 它很容易使用。 You can see how it works from that same download page. 您可以从同一个下载页面看到它的工作原理。

I created a couple javascript functions which i use to do the cursor management: 我创建了一些javascript函数,用于执行游标管理:

        function formatMoney(myField){
            if(myField.selectionStart || myField.selectionStart == '0'){
                var len = myField.value.length;
                var caretPos = doGetCaretPosition(myField);
                myField.value = accounting.formatMoney(myField.value);
                var newlen = myField.value.length;
                setCaretPosition(myField, newlen != len ? (newlen > len ? caretPos + 1 : caretPos - 1) : caretPos);
            }
        }
        function doGetCaretPosition (ctrl) {

            var CaretPos = 0;
            // IE Support
            if (document.selection) {

                ctrl.focus ();
                var Sel = document.selection.createRange ();

                Sel.moveStart ('character', -ctrl.value.length);

                CaretPos = Sel.text.length;
            }
            // Firefox support
            else if (ctrl.selectionStart || ctrl.selectionStart == '0')
                CaretPos = ctrl.selectionStart;

            return (CaretPos);

        }
        function setCaretPosition(elem, caretPos) {
            elem.value = elem.value;
            if(elem != null) {
                if(elem.createTextRange) {
                    var range = elem.createTextRange();
                    range.move('character', caretPos);
                    range.select();
                }
                else {
                    if(elem.selectionStart) {
                        elem.focus();
                        elem.setSelectionRange(caretPos, caretPos);
                    }
                    else
                        elem.focus();
                }
            }
        }

You can use the functions with the text field as: 您可以将文本字段的功能用作:

<input id="myField" type="text" onkeyup="formatMoney(this);" onChange="formatMoney(this);" onBlur="formatMoney(this);" />

The getting and the setting caret position functions were gotten from here: Set keyboard caret position in html textbox 获取和设置插入位置函数从这里得到: 在html文本框中设置键盘插入位置

I tested it on Android 2.1 emulator. 我在Android 2.1模拟器上测试过它。 Though the emulator was slow but it seemed to be working. 虽然模拟器速度很慢,但似乎工作正常。 You can find the screenshot here: https://www.dropbox.com/s/9ximuwh64kadiea/shot.JPG?dl=0 你可以在这里找到截图: https//www.dropbox.com/s/9ximuwh64kadiea/shot.JPG?dl = 0

Here's an open source, well contributed, well commited library : https://github.com/plentz/jquery-maskmoney 这是一个开源,贡献良好,提交良好的库: https//github.com/plentz/jquery-maskmoney

Seems to answers your need, doesn't it ? 似乎回答你的需要,不是吗? Havent't tested it under Android though. 虽然没有在Android下测试它。

看来这个jQuery插件 - NumBox - 旨在为您的问题提供解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM