javascript中数组的Html datalist值 [英] Html datalist values from array in javascript

查看:112
本文介绍了javascript中数组的Html datalist值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的程序,它必须从服务器上的文本文件中获取值,然后在输入文本字段中填充数据列表作为选择。

I have a simple program which has to take the values from the text file on server and then populate on the datalist as the selection in the input text field.

为此,我想要采取的第一步是我想知道如何动态地将javascript数组用作数据列表选项。

For this purpose the first step i want to take is that i want to know that how the array of javascript can be used as a datalist options dynamically.

我的代码是:

<html>  
<script>

var mycars = new Array();
mycars[0]='Herr';
mycars[1]='Frau';

</script>

<input name="anrede" list="anrede" />
<datalist id="anrede">
 <option value= mycars[0]></option>
 <option value="Frau"></option> 
</datalist>
</html>

我想填充包含数据列表的输入文本字段作为数组的建议。在这里,我没有考虑数组值。实际上我不需要两个datalist选项,但dxnamic取决于数组长度

I want to populate the input text field containing the datalist as suggestion from the array. Also here I havenot take into account the array values. Actually i need not two datalist options but dxnamic depending on the array length

推荐答案

我不确定我是否清楚地理解你的问题。无论如何,试试这个:

I'm not sure if I understood your question clearly. Anyway, try this:

<input name="car" list="anrede" />
<datalist id="anrede"></datalist>

<script type="text/javascript">
  var mycars = new Array();
  mycars[0]='Herr';
  mycars[1]='Frau';

  var options = '';

  for(var i = 0; i < mycars.length; i++)
    options += '<option value="'+mycars[i]+'" />';

  document.getElementById('anrede').innerHTML = options;
</script>

这篇关于javascript中数组的Html datalist值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆