2016-01-12 41 views
1

我有兩個文本框#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實現這一點?

+0

你研究過這個嗎? –

+0

我已經更新了我的答案,我提出了。 –

+1

你能爲你的代碼提供小提琴 – Meenakshi

回答

2

我想你可以通過這個解決方案Click

我強調幾點。

使用谷歌翻譯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仍然是免費的

+0

已經嘗試過。 api已經貶值了。 –

+0

@BhuwanGautam:你的意思是你試過所有的解決方案?有三種解決方案:P –

+0

嗯..嘗試第一個。第三個鏈接不起作用。第二選擇看起來很有希會試一試。 –

相關問題