我试图将文本/段落从其原始英文版本翻译为谷歌女孩的谈话。例如,“嗨”将是“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