我試圖將文本/段落從其原始英文版本翻譯爲谷歌女孩的談話。例如,「嗨」將是「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"},.....]
可否請你具體談談 「不走」。究竟是什麼結果呢? –
@CindyMeister表示單詞不被替換 – Noobtastic
改變textarea值一次沒有意義......然後在每次從不同源的循環中迭代該值。此外,ID必須是唯一的,所以'$(「#textarea」)。each'是一種難以解決的問題 – charlietfl