javascript - 返回数据在特定的 if 语句中不起作用
问题描述
我正在使用我在下面生成的这个 javascript 翻译网页,我在转换输入中的文本时遇到问题,所以我尝试将 if 条件放在我的代码中,如果它是输入,则使用返回,如果不使用 .html,但我有问题退货是因为它不起作用而且我不知道为什么(请参阅代码中的注释以提供更多信息)
function changeLanguage(lang){
$.post("/misc/sessionUpdate.asp",{ 'lang': lang});
$.getJSON("/language/"+lang+".json", function( data ) {
$.each(data, function(property, val){
$('[data-localize]').val(function() {
type = $(this).attr("type");
attrVal = $(this).attr("data-localize");
// if put return data[property]; here it is working but the problem it should be inside the if(attrVal == property)
if(attrVal == property){
if (type == "submit"){
// i already tried console.log (attrVal +"=="+ property) to verify if there is an instance of true and there is
return data[property];
}else{
attrName = '[data-localize="'+property+'"]'; //sample format [data-localize=menu]
$(attrName).html(val); // data[property] is the corresponding value of the key in json
}
}
});
});
}).done(function(){
$('.languageLoader').css('display','none');
});
}
我还将算法放在 .done 函数中,以避免 ajax 中的异步问题,但仍然无法正常工作
提前谢谢各位....
解决方案
如下所示更新您的循环。
$.each(data, function(property, val){
$('[data-localize="' + property + '"]').each(function(index, item) {
type = $(this).attr("type");
if (type == "submit") {
$(this).val(data[property]);
} else {
$(this).html(val);
}
});
});
推荐阅读
- elasticsearch - 查找字段不存在或字段小于值的文档
- java - Tomcat - 不使用 Apache HTTPD 的嵌套 webapp 路径
- .net - En Dash 和 Start of Guarded Area 字符
- javascript - 如何添加通过 div 重叠的水平线?
- c++ - 如何从 openGL 屏幕获取位图标题?(视频截取)
- php - 如何使用数组在 Bootstrap 中创建下拉菜单?
- google-apps-script - 如何在单独的选项卡上插入一行并保留一列公式?
- azure - QnA Maker 普遍可用与预览:它找不到答案
- node.js - 找不到模块:错误:无法解析“路径”
- bash - 如何创建监听信号的 bash 脚本