仅将输入的字符从一个文本框复制到另一个文本框

时间:2016-01-12 11:20:48

标签: javascript jquery

我有两个文本框#textBox1#textBox2

用户将输入#textBox1,并且所有值都需要自动复制到#textBox2 [我这里有onkeyup个事件]。只复制和粘贴从一个到另一个不是问题。我的要求是,一旦用户按spaceenter我的#textBox1中的英语单词将转换为本地语言,但我只需#textBox2上的英文版。

请参阅以下示例:

textBox1 = "Bhuwan", textBox2="Bhuwan"

用户按空格键

textBox1 = "भुवन ", textBox2="Bhuwan "

textBox1 = "भुवन Gautam", textBox2="Bhuwan Gautam"

再一次用户按空格键

textBox1 = "भुवन गौतम", textBox2="Bhuwan Gautam"

我使用了以下内容,但如果用户在#textBox1上使用退格键,则表示不完整。

$("#textBox1").keyup(function(e) {
            $('#textBox2').val($('#textBox2').val()+String.fromCharCode(e.which));
});

我知道有更好的解决方案。我如何从jquery或javascript实现这一目标?

1 个答案:

答案 0 :(得分:2)

我认为您可以通过此解决方案Click

我将强调一些观点。

使用Google翻译API。使用方便。例如,以下内容将西班牙语翻译成英语。要翻译和翻译其他语言,只需更改'es'和'en'

google.load("language", "1");

function initialize() {
    var content = document.getElementById('content');
    content.innerHTML = '<div id="text">Hola, me alegro mucho de verte.<\/div><div id="translation"/>';
    var text = document.getElementById("text").innerHTML;
    google.language.translate(text, 'es', 'en', function(result) {
        var translated = document.getElementById("translation");
        if (result.translation) {
            translated.innerHTML = result.translation;
        }
    });
}
google.setOnLoadCallback(initialize);

尝试谷歌翻译http://code.google.com/apis/language/translate/overview.html

您也可以使用此 JQuery插件 http://www.openxrest.com/translatejs

您可以使用 bing翻译 .. http://setahost.com/bing-translate-api-with-jquery-ajax/ Bing api仍然是免费的

相关问题