[英]Adding values to a dropdown <select> box using JQuery
I have a issue with populating values in a box using JQuery. 我有一个问题,使用JQuery在框中填充值。
Instead of adding it underneath each other it adds it next to all other elements 它不是将它添加到彼此之下,而是将它添加到所有其他元素旁边
my code 我的代码
$('#pages option').append($('#t1').val());
I think you want 我想你想要的
$('#pages').append($('#t1').val());
assuming pages is the id of your <select>
. 假设pages是
<select>
的id。 Also, $('#t1').val()
should be an <option>
element, not a value. 另外,
$('#t1').val()
应该是<option>
元素,而不是值。 Something like this 像这样的东西
var newOption = $('<option value="'+val+'">'+val+'</option>');
$('#pages').append(newOption);
or 要么
var newOption = $('<option>');
newOption.attr('value',val).text(val);
$('#pages').append(newOption);
whichever is easier for you to read. 哪个更容易阅读。
Here's a Working Demo 这是一个工作演示
You probably want something along the lines of 你可能想要一些东西
$("#pages").append("<option>"+$("#t1").val()+"</option>");
That will make and append an option to your select box 这将为您的选择框制作并附加一个选项
Try this.. 尝试这个..
using jquery you can use 使用jquery你可以使用
this.$('select#myid').append('<option>newvalue</option>');
where " myid " is the id of the dropdown list and newvalue is the text that you want to insert.. 其中“ myid ”是下拉列表的id , newvalue是您要插入的文本。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.