简体   繁体   中英

How to set values in Input<input> and Select <select> tags using Javascript

I want to create an auto-set of values in input or select tag based on the condition provided in the java script below: My problem here is that whenever i submit the data i wont reflect in the database. Any idea or a snippets of code would a great help.

 function myFunction() { var condition = document.getElementById("course").value; var text if(condition==="BSCS"|| condition==="BSIT"|| condition==="BSIS"||condition==="BLIS"||condition==="BSEMC") { text="Department A"; } else if(condition==="BSA"||condition==="BSBA"||condition==="BSOA"||condition==="BSREM"||condition==="BSHRM"){ text="Department B"; }else if(condition==="BEED"||condition==="BSED"||condition==="BSSW"||condition==="AB-PolScie"||condition==="AB-Philo"||condition==="AB-English"){ text="Department C"; }else{ text="N/A"; } document.getElementById("demo").innerHTML=text; } 
 <select id="course" onchange="myFunction()" class="form-control" name="Course"> <option value="None">Select Course</option> <option value="BSCS">BSCS</option> <option value="BSIT">BSIT</option> </select> 

在此输入图像描述

You heve used innerHTML which is wrong if you want to set a value, use value .

document.getElementById("demo").innerHTML=text;

change to:

document.getElementById("demo").value=text;

 function myFunction() { var condition = document.getElementById("course").value; var text if(condition==="BSCS"|| condition==="BSIT"|| condition==="BSIS"||condition==="BLIS"||condition==="BSEMC") { text="Department A"; } else if(condition==="BSA"||condition==="BSBA"||condition==="BSOA"||condition==="BSREM"||condition==="BSHRM"){ text="Department B"; }else if(condition==="BEED"||condition==="BSED"||condition==="BSSW"||condition==="AB-PolScie"||condition==="AB-Philo"||condition==="AB-English"){ text="Department C"; }else{ text="N/A"; } document.getElementById("demo").value=text; } 
 <select id="course" onchange="myFunction()" class="form-control" name="Course"> <option value="None">Select Course</option> <option value="BSCS">BSCS</option> <option value="BSIT">BSIT</option> </select> <input id="demo" type="text" name="Dept" class="form-control" placeholder="Department" required> 

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