JavaScript验证不起作用它一直在告诉填写字段

时间:2015-11-19 07:37:33

标签: javascript html html5

我有一个这样的表格:

<form id="formDaftar" onsubmit="return daftarClick()"> 

    method="post"> <table id="tableFormDaftar" border="0" width="400px" height="300px"> <tr> <td>Nama</td> <td><input type="text" name="nama" id="nama" size="39"/></td> </tr>
    <tr> <td>Alamat</td> <td><textarea cols="40" rows="4" name="alamat" id="alamat"> </textarea></td> </tr> <tr> <td>Tempat/Tanggal Lahir</td> <td><input name="tempatLahir" id="tempatLahir" size="39" type="text"/></td> </tr>
    <tr> <td>Jenis Kelamin</td> <td><input type="radio" id="radio1" name="gender" id="gender1"/>Laki-laki <input type="radio" id="radio1" name="gender" id="gender2"/>Perempuan </td> </tr>
    <tr> <td>Pekerjaan</td> <td><input name="pekerjaan" id="pekerjaan" size="39"/></td> </tr>
    <tr> <td>Status</td> <td> <select id="status" name="status"> <option selected="selected">Lajang</option> <option>Menikah</option> <option>Duda</option> <option>Janda</option> </select></td> </tr>
    <tr> <td>Agama</td> <td><select id="agama" name="agama"> <option>Islam</option> <option selected="selected">Kristen</option> <option>Katholik</option> <option>Budha</option> <option>Hindu</option> </select></td> </tr> <tr> <td>Kewarganegaraan</td> <td><select id="kwn"> <option selected="selected">Indonesia</option> <option>Asing</option> </select></td> </tr> <tr> <td colspan="2" id="colbutton"> <button name="masukkan" type="submit" id="masukkan">Masukkan</button> <button type="reset" name="batalkan" id="batalkan">Batalkan</button></td> </tr> </table></form>

我有一个这样的脚本:

<script type="text/javascript"> 
var username = document.getElementById("username").value; 
var password = document.getElementById("password").value;
var nama = document.getElementById("nama").value;
var alamat = document.getElementById("alamat").value;
var tempatLahir = document.getElementById("tempatLahir").value;
var gender;

if (document.getElementById("gender1").checked){
    gender = document.getElementById("gender1").value;
}
else if (document.getElementById("gender2").checked){
    gender = document.getElementById("gender2").value;
}

var a = document.getElementById("agama");
var agama = a.options[a.selectedIndex].text;

var pekerjaan = document.getElementById("pekerjaan").value;

var b = document.getElementById("status");
var status = b.options[b.selectedIndex].text;

var c = document.getElementById("kwn");
var kwn = c.options[c.selectedIndex].text;
function loginClick(){ if (username == ""){ 
alert("Username harus diisi!"); return false; } 
else if (password == ""){ 
alert("Password harus diisi!"); return false; } }
function daftarClick(){
if (nama == ""){
    alert("Nama harus diisi!"); 
    return false;
}
else if (alamat == ""){
    alert("Alamat harus diisi!");
    return false;
}
else if (tempatLahir == ""){
    alert("Tempat/Tanggal Lahir harus diisi!");
    return false;
}
else if(!document.getElementById("gender1").checked && !document.getElementById("gender2").checked){
    alert("Jenis Kelamin harus dipilih!");
    return false;
}
} </script>

问题是,每次我提交表单时,它仍然会创建一个警告,说我需要填写表单,即使我已经提交了所有字段。在这里需要一些帮助,我完全是初学者。

0 个答案:

没有答案