Javascript:使用 javascript 创建下拉列表时出现问题?

发布于 2024-11-06 06:48:26 字数 1011 浏览 0 评论 0原文

我正在尝试使用 javascript 从覆盖层(div 元素)上的数组创建一个下拉列表。

在此示例中,

spcodelist = [u'CA125', u'HCM112', u'HCM147', u'HCM97', u'HKI128', u'HKI158', u'HKS161', u'HKS231', u'TKA230']

以下是相关的代码行:

var pcode_form = document.createElement('form');
div.appendChild(pcode_form);
var pcode_select = document.createElement('select');
pcode_form.appendChild(pcode_select);
var i = 0;
var spcodelist = document.getElementById('spcodelist').value;
spcodelist = spcodelist.replace("[","");
spcodelist = spcodelist.replace("]","");
var spcodearr = new Array();
spcodearr = spcodelist.split(', ');
for (i=0;i<=spcodearr.length;i++){
    spcodearr[i] = spcodearr[i].replace(/u'/g,"");
    spcodearr[i] = spcodearr[i].replace(/'/g,"");
    var pcode_option = document.createElement('option');
    pcode_option.text = spcodearr[i];
    pcode_option.value = spcodearr[i];
    pcode_select.appendChild(pcode_option);
}

使用此代码,下拉列表工作正常,但后面的代码将不再工作。我不知道有什么问题?我该如何解决?或者有什么更好的解决方案吗?非常感谢。

I'm trying to create a dropdown list from an array on my overlay (div element) using javascript.

In this example,

spcodelist = [u'CA125', u'HCM112', u'HCM147', u'HCM97', u'HKI128', u'HKI158', u'HKS161', u'HKS231', u'TKA230']

Here are related lines of code:

var pcode_form = document.createElement('form');
div.appendChild(pcode_form);
var pcode_select = document.createElement('select');
pcode_form.appendChild(pcode_select);
var i = 0;
var spcodelist = document.getElementById('spcodelist').value;
spcodelist = spcodelist.replace("[","");
spcodelist = spcodelist.replace("]","");
var spcodearr = new Array();
spcodearr = spcodelist.split(', ');
for (i=0;i<=spcodearr.length;i++){
    spcodearr[i] = spcodearr[i].replace(/u'/g,"");
    spcodearr[i] = spcodearr[i].replace(/'/g,"");
    var pcode_option = document.createElement('option');
    pcode_option.text = spcodearr[i];
    pcode_option.value = spcodearr[i];
    pcode_select.appendChild(pcode_option);
}

With this code, the dropdown list works fine but code after it will not work any more. I don't know what's the problem? How can I solve it? Or is there any better solution? Thank you very much.

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

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

发布评论

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

评论(1

落花浅忆 2024-11-13 06:48:26

我不知道你做错了什么,但以下内容工作正常并且应该在任何浏览器中工作。我添加了一个删除按钮,以便您可以根据需要随时添加和删除选择。

<script type="text/javascript">

function addSelect(id) {

  var div = document.getElementById(id);
  var pcode_form = document.createElement('form');
  pcode_form.id = 'f0';
  div.appendChild(pcode_form);
  var pcode_select = document.createElement('select');
  pcode_form.appendChild(pcode_select);

  var spcodelist = document.getElementById('spcodelist').value;

  // Do replaces in one go
  spcodelist = spcodelist.replace(/[\[\]\'u ]/g,'');

  var spcodearr = spcodelist.split(',');

  for (var i=0, iLen=spcodearr.length; i<iLen; i++) {
    pcode_select.options[i] = new Option(spcodearr[i],spcodearr[i]);
  }
}

</script>

<button onclick="addSelect('d0');">Add select</button>
<button onclick="
  var el = document.getElementById('f0');
  el.parentNode.removeChild(el);
">Remove form</button>
<div id="d0"></div>

<textarea id="spcodelist" style="display:none">[u'CA125', u'HCM112', u'HCM147', u'HCM97', u'HKI128', u'HKI158', u'HKS161', u'HKS231', u'TKA230']</textarea>

I don't know what you are doing wrong, but the following works fine and should work in any browser. I've added a remove button so you can add and remove the select as often as you like.

<script type="text/javascript">

function addSelect(id) {

  var div = document.getElementById(id);
  var pcode_form = document.createElement('form');
  pcode_form.id = 'f0';
  div.appendChild(pcode_form);
  var pcode_select = document.createElement('select');
  pcode_form.appendChild(pcode_select);

  var spcodelist = document.getElementById('spcodelist').value;

  // Do replaces in one go
  spcodelist = spcodelist.replace(/[\[\]\'u ]/g,'');

  var spcodearr = spcodelist.split(',');

  for (var i=0, iLen=spcodearr.length; i<iLen; i++) {
    pcode_select.options[i] = new Option(spcodearr[i],spcodearr[i]);
  }
}

</script>

<button onclick="addSelect('d0');">Add select</button>
<button onclick="
  var el = document.getElementById('f0');
  el.parentNode.removeChild(el);
">Remove form</button>
<div id="d0"></div>

<textarea id="spcodelist" style="display:none">[u'CA125', u'HCM112', u'HCM147', u'HCM97', u'HKI128', u'HKI158', u'HKS161', u'HKS231', u'TKA230']</textarea>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文