简体   繁体   English

动态填充选择标签不起作用

[英]Dynamically populating select tag does not work

I have some code for dynamically populating select options. 我有一些代码用于动态填充选择选项。 It works when I use this in a plain text file locally. 当我在本地纯文本文件中使用此功能时,它将起作用。 However this does not work in my wordpress based site. 但是,这在我的基于wordpress的网站中不起作用。 Here is the code : 这是代码:

<div class="postcell">
  <select onchange="populate1(this)" id="toplevel">
    <option>SEÇİNİZ</option>
    <option value="Keçiören">Keçiören</option>
  </select>
</div>
<div class="postcell">
  <select  id="selectlevel2"></select>
</div>
<script type="text/javascript">
  function populate1(selectp)
  {
    selectc=document.getElementById('selectlevel2');
    if(!selectc){return;}   

    var content=new Array();
    content['Keçiören']=['BLABLA', 'araba'];
    selectc.options.length=0;
    cur=content[selectp.options[selectp.selectedIndex].value];
    if(!cur){return;}

    selectc.options.length=cur.length;
    for(var i=0;i<cur.length;i++)
    {
      selectc.options[i].text=cur[i];
      selectc.options[i].value=cur[i];
    }

    alert(selectc.options[0].text);
    alert(selectc.options[1].text);
  }

</script>

I am aware the code is a bit messy. 我知道代码有点混乱。 But it is dynamically created, so I could not help it much. 但是它是动态创建的,因此我无济于事。 The part at the last 4-5 lines ( ones with alert ) are to control if I am executing the function. 最后4-5行(带有alert的行)的部分是控制我是否正在执行该功能。 And as it seems, I am executing the function. 看起来,我正在执行该功能。 Alerts pop up, and also with the expected content in them. 警报将弹出,并且其中包含预期的内容。 It also prooves for loop works fine. 它也证明循环工作正常。

Also, I have checked and validated that there are no js errors, in firebug. 另外,我已经检查并验证Firebug中没有js错误。

I am really stuck, if this does not work, I will have to change a bunch of other codes. 我真的很困惑,如果这行不通,我将不得不更改其他代码。 I will appreciate any help. 我将不胜感激。 Thank you. 谢谢。

------EDIT------ - - - 编辑 - - -

I have just realized, i did not explain the problem -.-. 我刚刚意识到,我没有解释问题-.-。 The problem is i can get correct content in those controlled alert boxes. 问题是我可以在那些受控警报框中获取正确的内容。 But the second select box's content will not change as expected. 但是第二个选择框的内容不会按预期更改。 What I was trying to do is, to change the 2nd select box's option based on what is selected in the first select box. 我想做的是根据第一个选择框中的选择更改第二个选择框的选项。 First one's id is "toplevel", second one has "selectlevel2" 第一个的ID为“ toplevel”,第二个的ID为“ selectlevel2”

I ALSO WANT TO ADD: 我还想添加:

The template automatically adds the following lines under the select tags : 模板会自动在选择标签下添加以下几行:

<div id="selectlevel2_chzn" class="chzn-container chzn-container-single chzn-container-single-nosearch" style="width: 220px;" title="">
<a class="chzn-single" tabindex="-1" href="javascript:void(0)">
<span>Seçiniz</span>
<div>

So If I dynamically add new elements it still overrides "Seçiniz" on them. 因此,如果我动态添加新元素,它仍然会覆盖它们上的“Seçiniz”。 ( Which I have decided in the php codes as the first defaul element, which means "Please Select". ). (这是我在php代码中确定的第一个默认元素,表示“请选择”。)。 I think whatever those lines is it is about something called " Chosen ". 我认为这些行是关于“选择”的。 How is it possible to add such a div tag under every select tags automatically? 如何在每个选择标签下自动添加这样的div标签?

SelectElement.options is a read-only variable. SelectElement.options是只读变量。 ( mdn ). mdn )。 What you are trying to do doesn't work. 您尝试执行的操作无效。 You'll need to use something like the following code: 您需要使用类似以下代码的内容:

  function populate1(selectp)
  {
    var selectc=document.getElementById('selectlevel2');
    if(!selectc){return;}   

    var content=new Array();
    content['Keçiören']=['BLABLA', 'araba'];
    selectc.options.length=0;
    cur=content[selectp.options[selectp.selectedIndex].value];
    if(!cur){return;}

    //Clear all options
    selectc.innerHTML = "";

    selectc.options.length=cur.length;
    for(var i=0;i<cur.length;i++)
    {
      var tag = document.createElement('option');
      tag.innerText=cur[i];
      tag.value=cur[i];
      console.log( tag );
      selectc.appendChild( tag );
    }
  }

On the first line, this uses 'var', as you previously leaked it in the global scope. 在第一行,它使用'var',因为您之前在全局范围中泄漏了它。 In the loop it now uses document.createElement ( mdn ) and Element.appendChild ( mdn ). 在循环中,它现在使用document.createElementmdn )和Element.appendChildmdn )。

Fiddle: http://jsfiddle.net/WYBX9/ 小提琴: http//jsfiddle.net/WYBX9/

I solved my problem. 我解决了我的问题。 It seems that, they declared a css file name "chosen" for select tags. 看来,他们为选择标签声明了一个“选择”的css文件名。 I think it is a public library. 我认为这是一个公共图书馆。 So somehow, I still can not understand how, it adds those div tags after every select element. 所以以某种方式,我仍然不明白如何在每个select元素之后添加那些div标签。 Weird. 奇怪的。 I deleted the library from the template folders : D. Which let me use standard elements and then the code worked. 我从模板文件夹D中删除了该库。D.我可以使用标准元素,然后使代码工作。

Edit : Further research showed me that there is a select box plugin named chosen. 编辑:进一步的研究表明,有一个名为selected的选择框插件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM