简体   繁体   中英

Weird JavaScript behaviour with variable/alert

I'm trying to get some variable from a html form. I can read all of the values except for one, on which I do absolutely nothing different.

I tried to console.log starttijd , it shows nothing in my console. But when I alert starttijd it does show me the right value. This is the code I'm running:

 function validateGroepslesForm() { var message = ""; var date = document.forms["groepslesform"]["date"].value; if (!date) { message += "Vul een geldige datum in<br>"; } var endtime = document.forms["groepslesform"]["endtime"].value; if (endtime == "") { message += "Vul een geldige eindtijd in<br>"; } var starttijd = document.forms["groepslesform"]["starttime"].value; if (starttijd = "") { message += "Vul een geldige starttijd in<br>"; } console.log(starttijd); var participants = document.forms["groepslesform"]["participants"].value; if (participants == "0") { message += "Vul een geldig aantal deelnemers in<br>"; } 
 <p>Datum:</p> <input type="date" name="date" id="futuredate" value="" /> <p>Start tijd:</p> <input type="text" name="starttime" id="starttime" placeholder="Start tijd van de groepsles" value="" /> <p>Eind tijd:</p> <input type="text" name="endtime" id="endtime" placeholder="Eind tijd van de groepsles" value="" /> <p>Maximaal aantal deelnmers</p> <input type="number" name="participants" id="participants" min="0" value="16" /> 

Your comparison check is assignment in the code. Change to:

if (starttijd == "") {
    message+= "Vul een geldige starttijd in <br>";
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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