使用 javascript 附加选择选项

发布于 2024-11-29 05:24:22 字数 471 浏览 0 评论 0原文

我目前有一个空的下拉列表,我想用 javascript 从数据库中获取的一些数据来填充它。
我真的不知道该怎么做,所以现在我正在尝试这个。
JavaScript 与 JQuery。

for (var i=0;i<data.length;i++)
  {
  var code$=data[i].String1;
  var desc$=data[i].Code_Description;
  var str$=$('<option value='+code$+'>'+desc$+'</option>');
  option$.append(str$);
  }
   $('#offCode').html(option$); 

我也尝试过

$('#offCode').append(option$); 

,但这些都不起作用

谢谢

i currently have an empty drop down list that I would like to fill with some data I got from my database using javascript.
I really have no idea how to do this so right now I am trying this.
Javascript with JQuery.

for (var i=0;i<data.length;i++)
  {
  var code$=data[i].String1;
  var desc$=data[i].Code_Description;
  var str$=$('<option value='+code$+'>'+desc$+'</option>');
  option$.append(str$);
  }
   $('#offCode').html(option$); 

I have also tried

$('#offCode').append(option$); 

but none of this works

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

仅冇旳回忆 2024-12-06 05:24:22

尝试使用类似的内容:

for (var i=0;i<data.length;i++) {
      var code$=data[i].String1;
      var desc$=data[i].Code_Description;
      $('<option value='+code$+'>'+desc$+'</option>').appendTo($('#idOfSelectElement'));
}

显然,上面的内容依赖于从 ajax/数据库调用中正确检索和填充的变量。

Try using something like:

for (var i=0;i<data.length;i++) {
      var code$=data[i].String1;
      var desc$=data[i].Code_Description;
      $('<option value='+code$+'>'+desc$+'</option>').appendTo($('#idOfSelectElement'));
}

The above, obviously, relies on the variables being properly retrieved and filled from your ajax/database-call.

可遇━不可求 2024-12-06 05:24:22

append() 将您要创建的选项元素直接添加到

您无法从代码中看出 option$ 是什么片段,但 append 将参数作为子元素插入。看起来您将所有选项append'ing到一个元素,然后将该元素append'ing到一个选择,这是行不通的。

append() the option elements you're making directly to the <select>

You can't tell what option$ is from the code snippet, but append inserts the argument as a child element. It would appear that you're append'ing all the options to an element, then append'ing that element to a select, which would not work.

|煩躁 2024-12-06 05:24:22

下面的解决方案大致基于您的问题中提供的信息。由于未披露代码的完整范围,某些项目可能不适用。因此,该解决方案略有修改,但您应该能够轻松修改它以满足您的需求。

HTML:

<select></select>
<br />
<h1>Your select box options code:</h1>
<div id="code"></div>

JavaScript:

var data = {"info":[{"String1":"string-value1","Code_Description":"description1"},{"String1":"string-value2","Code_Description":"description2"},{"String1":"string-value3","Code_Description":"description3"},{"String1":"string-value4","Code_Description":"description4"}]};

data = data.info;

for (var i=0;i<data.length;i++){
    var e = data[i];
    var code = e.String1;   
    var desc = e.Code_Description;   
    var str =('<option value="'+code+'">'+desc+'</option>');
    $("select").append(str); 
} 

$("#code").text($("select").html());

工作示例位于:http://jsfiddle.net/nAPDq/

The solution below is loosely based off of the information provided within your question. Some items may not be applicable as the full scope of your code was not disclosed. As a result, the solution is slightly modified but you should be able to easily modify it to fit your needs.

HTML:

<select></select>
<br />
<h1>Your select box options code:</h1>
<div id="code"></div>

JavaScript:

var data = {"info":[{"String1":"string-value1","Code_Description":"description1"},{"String1":"string-value2","Code_Description":"description2"},{"String1":"string-value3","Code_Description":"description3"},{"String1":"string-value4","Code_Description":"description4"}]};

data = data.info;

for (var i=0;i<data.length;i++){
    var e = data[i];
    var code = e.String1;   
    var desc = e.Code_Description;   
    var str =('<option value="'+code+'">'+desc+'</option>');
    $("select").append(str); 
} 

$("#code").text($("select").html());

Working example at: http://jsfiddle.net/nAPDq/

东京女 2024-12-06 05:24:22
$("<option value='abc'>Whatever Text</option>").appendTo($('select'));
$("<option value='abc'>Whatever Text</option>").appendTo($('select'));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文