在下拉列表中获取选定的值

时间:2020-04-22 14:49:00

标签: javascript html json

我想按下一个按钮来更改页面上的语言,但是我遇到了问题。 我不明白如何实现“选择”-“选项”。

这是代码JS:

function checkAlert(evt) {
    var strUser = evt.options[evt.selectedIndex].value;
    Traduction(strUser);
}

function Traduction(index) {
    var xhttp = new XMLHttpRequest();
    xhttp.onreadystatechange = function () {
        if (this.readyState == 4 && this.status == 200) {
            ShowJson(this.responseText);
        }
    };
    var counter;
    switch (index) {
        case 1:
            counter = "./Traduction/Fr.json";
            break;
        case 2:
            counter = "./Traduction/Nl.json";
            break;
        case 3:
            counter = "./Traduction/En.json";
            break;
        default:
            counter = "./Traduction/En.json";
    }
    xhttp.open("GET", counter, true);
    xhttp.send();
}
function ShowJson(JsonData) {

    var obj = JSON.parse(JsonData);
    document.getElementById("p1").innerHTML = obj.p1;
    document.getElementById("p2").innerHTML = obj.p2;

}

这是HTML:

    <div class="Title" id="TextTitle">
        <h1>This is the title</h1>
        <div class="Menu">
            <select class="Language" id='Language' onchange="checkAlert(this);">
                <option value='1'>FR</option>
                <option value='2'>NL</option>
                <option value='3' selected='selected'>EN</option>
            </select>
            <select class="Style" id='Style'>
                <option value='1' selected='selected'>Style 1</option>
                <option value='2'>Style 2</option>
                <option value='3'>Style 3</option>
            </select>
        </div>
    </div>

能帮我吗? 谢谢

0 个答案:

没有答案