javascript - Add value to a drop down menu with a form - Stack Overflow

Is there a way to insert values in a drop down menu with a simple form? I have this drop down menu<f

Is there a way to insert values in a drop down menu with a simple form? I have this drop down menu

<form>
  <select name="users" onchange="showUser(this.value)">
    <option value="">Select a category:</option>
    <option value="movie">Movie</option>
    <option value="musik">Musik</option>
    <option value="books">Books</option>
    <option value="images">Images</option>
  </select>
</form>

Should I use an array? I been searching the net but couldn't find anything that would help.

Is there a way to insert values in a drop down menu with a simple form? I have this drop down menu

<form>
  <select name="users" onchange="showUser(this.value)">
    <option value="">Select a category:</option>
    <option value="movie">Movie</option>
    <option value="musik">Musik</option>
    <option value="books">Books</option>
    <option value="images">Images</option>
  </select>
</form>

Should I use an array? I been searching the net but couldn't find anything that would help.

Share Improve this question edited Jul 14, 2012 at 13:56 Andrew Marshall 97.1k20 gold badges227 silver badges217 bronze badges asked Jul 14, 2012 at 13:50 DymondDymond 2,2877 gold badges46 silver badges82 bronze badges 3
  • 2 What does this mean? What form? How are you adding them? Client-side with Javascript? – Rudi Visser Commented Jul 14, 2012 at 13:53
  • Now I just add them in the code, I want to be able to use a simple form to add some other values. Java script should do it, but i really dont know how – Dymond Commented Jul 14, 2012 at 13:54
  • @FelipeOtarola If you want Javascript, then tag your question so instead of as PHP. Also, your ment doesn't clarify your question very much. – Andrew Marshall Commented Jul 14, 2012 at 13:56
Add a ment  | 

2 Answers 2

Reset to default 5

Here is a JavaScript solution to add items to a drop down.

The HTML to use..

 <form>
    <select id="categories" name="users" onchange="showUser(this.value)">
    <option value="">Select a category:</option>
    <option value="movie">Movie</option>
    <option value="musik">Musik</option>
    <option value="books">Books</option>
    <option value="images">Images</option>
    </select>
    </form>​​

JavaScript code...

var categories = document.getElementById("categories");

var newOption = document.createElement('option');
newOption.innerText = "New value";
newOption.setAttribute('value', 'newvalue');

categories.appendChild(newOption);
​

jQuery code... (a lot shorter)

$("#categories").append("<option value='newvalue'>New Value</option>");

Here is the JavaScript JSFiddle and the jQuery JSFiddle to play with!

You could look into jQuery append() and option() with click event...

I've just created a working solution for you. Don't know if this is what you was looking for, but this is how it works:
You simply just write the value and visual option name into a form text field: value, text which gives you this:
<option value="value">text</option>

<select id="mySelect"></select>
<p>&nbsp;</p>
<input type="text" id="string"><br>
<button id="append">Append</button>


$('#append').click(function(){
    var str = $('#string').val();
    var substr = str.split(', ');
    $('#mySelect').append( new Option(substr[0],substr[1]));
});

Here is a working version: http://jsfiddle/SCf8m/1/

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745106851a4611610.html

相关推荐

  • javascript - Add value to a drop down menu with a form - Stack Overflow

    Is there a way to insert values in a drop down menu with a simple form? I have this drop down menu<f

    12小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信