2016-03-06 70 views
0

我試圖將文本/段落從其原始英文版本翻譯爲谷歌女孩的談話。例如,「嗨」將是「hellloooooo,什麼是?」。這些詞已經被輸入到一個數據庫中,並帶有英文單詞及其翻譯的谷歌女孩版本。例如:英文欄 - >你好,VG欄 - > hellloooooo,英文 - >是的,VG - >像,地獄是jQuery:在textarea段落中查找單詞並用單詞替換它

我正在使用MySQL從數據庫中獲取翻譯的單詞並返回一個JSON。

所以這是我如何AJAX取回:

$.ajax({ 
    type: "GET", 
    url: "dictionary.php", 
    success: function(data) { 
     var json = JSON.parse(data); 
     // replacing all the ing's with in' 
     $("#textarea").each(function(){ 
      var ing = $(this).val().replace(/ing/g,"in'"); 
      $(this).val(ing); 
      // finding the English words and replacing with VG 
      $.each(json, function(idx, obj) { 
       var vg= $("#content").val().replace(/obj.english/g, obj.vg); 
       $("#textarea").val(vg); 
      ); 
     });     
    } 
}); 

我得到的「ing」的替代品很好,工作,但想取代谷女孩的話是一個沒有去。我是否錯誤地循環瀏覽json對象?

編輯:這裏是JSON數據

[{"english":"hi","0":"hi","vg":"hellloooooo!","1":"hellloooooo"}, {"english":"yes","0":"yes","vg":"like, hells yes","1":"like, hells yes"},.....] 
+0

可否請你具體談談 「不走」。究竟是什麼結果呢? –

+0

@CindyMeister表示單詞不被替換 – Noobtastic

+0

改變textarea值一次沒有意義......然後在每次從不同源的循環中迭代該值。此外,ID必須是唯一的,所以'$(「#textarea」)。each'是一種難以解決的問題 – charlietfl

回答

0

請看這個替換單詞的小例子,然後輸出到一個新的字段,這可能有助於你想要達到的目的。

它遍歷每個字,去一個確切的詞匹配,你可能會遇到的問題,如果一些翻譯的也包含英語

$(document).ready(function(){ 
 
    
 
var json = [{"english":"hi","0":"hi","vg":"hellloooooo!","1":"hellloooooo"}, {"english":"yes","0":"yes","vg":"like, hells yes","1":"like, hells yes"}]; 
 

 
var userInput = $('#textarea').val(); 
 

 
json.forEach(function(word){ 
 
    console.log(word); 
 
    userInput = userInput.replace(new RegExp("\\b"+word.english+"\\b","g"),word.vg); 
 
}); 
 
    $('#translation').val(userInput); 
 
});
<!DOCTYPE html> 
 
<html> 
 
<head> 
 
<script src="https://code.jquery.com/jquery-2.1.4.js"></script> 
 
    <meta charset="utf-8"> 
 
    <meta name="viewport" content="width=device-width"> 
 
    <title>JS Bin</title> 
 
</head> 
 
<body> 
 
    <textarea id="textarea"> 
 
    hi this is a test of yes translation. hi yes 
 
    </textarea> 
 
    <textarea id="translation"> 
 
    
 
    </textarea> 
 
    </body> 
 
</html>

0

你的代碼似乎有幾個邏輯錯誤。你正在循環使用id的jQuery選擇?一個id總是被視爲唯一的,不管你做什麼,因此它被稱爲id :)然後你每次都替換html元素的值(在這種情況下恰好是一次,但這是你的錯誤承擔)。

0

話,我改變了你的HTML中這樣你的代碼才能正常工作。

HTML:

<textarea class="text"> 
      hi this is a text area what is going on? hi how are you? yes, I am fine. 
</textarea> 

<textarea class="text"> 
      hi this is a text area what is going on? hi how are you? yes, I am fine. 
</textarea> 

的JavaScript/JQuery的:

$.ajax({ 
      url: "dictionary.php", 
      type:"GET", 
      success: function(data) { 

      //store the parsed json 
      var json = JSON.parse(data);     


     //loop through the .text elements 
     $(".text").each(function(){//begin outer each 

      //reference the current object 
      var that = $(this); 

      //replace the all of the ing's with in' 
      var ing = $(this).val().replace(/ing/g,"in'"); 

      $(this).val(ing); 

      // finding the English words and replacing with VG 
      $.each(json, function(idx, obj) {//begin inner each 

       //Create a new regular expression that matches the word only. 
       var regExp = new RegExp("\\b" + obj.english,"g"); 

       //Replace the value of the text value with the vg value and store it. 
       var vg = that.val().replace(regExp, obj.vg); 

       //Replace the value of the .text text area in the dom. 
       that.val(vg); 


      });//<-- was missing a closing } bracket. //end inner each 

     });//end outer each  


      } 
}); 
相關問題