简体   繁体   中英

Add items to listbox using javascript

I am trying to add few items to a list box from a text box using javascript. but the problem is as soon as I am adding the items, it gets visible in the list box for a second and then gets deleted. Any solution for this?

I am adding items from TextBox4 to ListBox1

function AddToList() {

    // Create an Option object        

    var opt = document.createElement("option");

    // Add an Option object to List Box

    document.getElementById("ListBox1").options.add(opt);
    opt.text = document.getElementById("TextBox4").value;
    opt.value = document.getElementById("TextBox4").value;
    document.getElementById("ListBox1").options.add(opt);


}
document.getElementById("ListBox1").options.add(opt); // remove this line
opt.text = document.getElementById("TextBox4").value;
opt.value = document.getElementById("TextBox4").value;
document.getElementById("ListBox1").options.add(opt); 

As you're appending an <option> which has no value or text .

Got the solution, Need to add

return false;

at the end of the function.

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