我编写了一个代码,其中JQuery Ajax将调用JSON并基于所选的单选按钮,字段值应该更改。守则如下
$(document).ready(function(){
$.ajax({
type: "POST",
url: "test.json",
data : "",
dataType: "json",
contentType : 'application/json',
success: function (response) {
console.log(response);
try{
console.log(response.lang.english.login);
console.log(response.lang.arabic.login);
}
catch(Exception){
}
},
error: function (jqXHR, exception) { CRDates
alert("Error");
}
});
和JSON文件如下
{"lang":
{
"english":{
"login":"Login"
},
"arabic":{
"login":"دخول"
}}
and the HTML is as follows
选择一种语言:
<input type="radio" name="radio-choice" id="Language" value="Arabic"/>
<label for="radio-choice-1">Arabic</label>
<input type="radio" name="radio-choice" id="Language" value="Arabic"/>
<label for="radio-choice-4">English</label>
<div><label for="declarationNumber" style="margin-left:30px"><b>Login</div><br>
现在我想根据选择的单选按钮更改语言。我无法获得单选按钮的值。例如,如果我检查阿拉伯语,它应该改为阿拉伯语 请帮忙
答案 0 :(得分:1)
您可以使用以下代码获取单选按钮的值
var selectedVal = "";
var selected = $("input[type='radio']:checked");
if (selected.length > 0) {
selectedVal = selected.val();
}
答案 1 :(得分:1)
您可以尝试这样的事情:
的Javascript
var languages = null;
$(document).ready(function(){
// do your ajax here and set the languages variable
languages = {"lang":
{
"english":{
"login":"Login"
},
"arabic":{
"login":"دخول"
}
}
};
$('input[name="radio-choice"]').click(function(){
if ($(this).is(':checked')) {
$('#login-label b').text(languages.lang[$(this).data('language')].login);
}
});
});
HTML:
<input type="radio" name="radio-choice" id="Language-arabic" data-language="arabic" value="Arabic"/>
<label for="radio-choice-1">Arabic</label>
<input type="radio" name="radio-choice" id="Language-english" data-language="english" value="English"/>
<label for="radio-choice-4">English</label>
<div><label for="declarationNumber" style="margin-left:30px" id="login-label"><b>Login</b></label></div>