脚本选择更改仅运行一次

时间:2018-11-06 14:33:17

标签: javascript

我有一个JS文件,当从选择器中选择一个选项时,该文件会隐藏/显示字段。但是,当我选择其他选项并尝试返回上一个选项时,则不会发生更改。 html选择器值是您在if语句中看到的值。删除了一些变量,以便我可以在此处发布代码。

JS

function change(obj) {
    var selectBox = obj;
    var selected = selectBox.options[selectBox.selectedIndex].value;
    var num_floor = document.getElementById("num_floor");
    var sq_floor = document.getElementById("sq_floor");
    var podrum = document.getElementById("podrum");
    var br_balkon = document.getElementById("br_balkon");
    var lift = document.getElementById("lift");
    var sprat = document.getElementById("sprat");
    var ori = document.getElementById("ori");
    var year_built = document.getElementById("year_built");
    var parking = document.getElementById("parking");
    var num_rooms = document.getElementById("num_rooms");
    var num_toilets = document.getElementById("num_toilets");


    if(selected == 'Куќа') {
        lift.style.display = "none";
        iyear_built.required = true;
        iparking.required = true;
        isqmeter.required = true;
        inum_rooms.required = true;
        inum_toilets.required = true;
        inum_floor.required = true;
        isq_floor.required = true;
        ipodrum.required = true;
        ibr_balkon.required = true;
        isprat.required = true;
        iori.required = true;
    } else if(selected == 'Стан') {
        sq_floor.style.display = "none";
        num_floor.style.display = "none";
        ilift.required = true;
        iyear_built.required = true;
        iparking.required = true;
        isqmeter.required = true;
        inum_rooms.required = true;
        inum_toilets.required = true;
        ipodrum.required = true;
        ibr_balkon.required = true;
        isprat.required = true;
        iori.required = true;
    } else if(selected == 'Соба') {
        num_floor.style.display = "none";
        ori.style.display = "none";
        sq_floor.style.display = "none";
        br_balkon.style.display = "none";
        podrum.style.display = "none";
        lift.style.display = "none";
        num_rooms.style.display = "none";
        num_toilets.style.display = "none";
        parking.style.display = "none";
        year_built.style.display = "none";
        sprat.style.display = "none";
    } else if(selected == 'Викендица') {
        lift.style.display = "none";
        sprat.style.display = "none";
        iyear_built.required = true;
        iparking.required = true;
        isqmeter.required = true;
        inum_rooms.required = true;
        inum_toilets.required = true;
        inum_floor.required = true;
        isq_floor.required = true;
        ipodrum.required = true;
        ibr_balkon.required = true;
        iori.required = true;
    } 
} 

0 个答案:

没有答案