我在索引页面中创建了下拉列表我想从该列表中选择一个值,如果没有选择任何代码值,则验证它。
// According to MTL format (http://paulbourke.net/dataformats/mtl/):
// d is dissolve for current material
// factor of 1.0 is fully opaque, a factor of 0 is fully dissolved (completely transparent)

<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>
&#13;
但我不知道如何使这个循环版本。对于帮助和想法将是愉快的。 TY(抱歉英文不好)
答案 0 :(得分:0)
请注意,唯一改变的是document.getElementById("...")
中的文字。因此,您可以使用包含三个文本项的数组:["etnik","Jantina","warganegara"]
。并循环遍历:
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.");
}
}