简体   繁体   English

请帮助我为此创建一个循环版本

[英]Please help me to create a loop version of this

I have created the dropdown list in my index page i want to select the one value from that list and validate it if not selected any value the code for that. 我已经在索引页面中创建了下拉列表,我想从该列表中选择一个值,如果未选择该代码的任何值,则对其进行验证。

 <script> function Validate() { var b = document.getElementById("etnik"); var strUser = b.options[b.selectedIndex].value; var strUser1 = b.options[b.selectedIndex].text; if (strUser == 0) { alert("Sila isikan ruangan yang kosong."); } } function Validate1() { var a = document.getElementById("Jantina"); var strUser = a.options[a.selectedIndex].value; var strUser1 = a.options[a.selectedIndex].text; if (strUser == 0) { alert("Sila isikan ruangan yang kosong."); } } function Validate2() { var c = document.getElementById("warganegara"); var strUser = c.options[c.selectedIndex].value; var strUser1 = c.options[c.selectedIndex].text; if (strUser == 0) { alert("Sila isikan ruangan yang kosong."); } } </script> 
 <body> <form> <select id="Jantina" name="Jantina"> <option value='0'>Sila Pilih</option> <option value='1'>Lelaki</option> <option value='2'>Perempuan</option> </select> <br> <select id="etnik" name="etnik"> <option value='0'>Sila Pilih</option> <option value='1'>Melayu</option> <option value='2'>India</option> </select> <br> <select id="warganegara" name="warganegara"> <option value='0'>Sila Pilih</option> <option value='1'>Yes</option> <option value='2'>Tidak</option> </select> <input name="Simpan" type="submit" value="Simpan" onClick="Validate()" onclick="Validate1()" onclick="Validate2()"> </form> </body> 

But i dont know how to make this to loop version. 但我不知道如何使此循环版本。 It will be pleasure for the help and idea. 希望得到帮助和想法。 TY (sorry for bad in english) TY(对不起,英语不好)

Notice that the only thing that is changing is the text in document.getElementById("...") . 请注意,唯一更改的是document.getElementById("...")的文本。 So you can use an array with the three text items: ["etnik","Jantina","warganegara"] . 因此,您可以将数组与三个文本项一起使用: ["etnik","Jantina","warganegara"] And loop through them: 并遍历它们:

var array = ["etnik","Jantina","warganegara"];

for(var i = 0; i < array.length; i++)
{
    var a = document.getElementById(array[i]);
    var strUser = a.options[a.selectedIndex].value;
    var strUser1 = a.options[a.selectedIndex].text;
    if (strUser == 0) {
        alert("Sila isikan ruangan yang kosong.");
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM