【问题标题】:jQuery function to fill up a form won't work填写表格的jQuery函数不起作用
【发布时间】:2013-10-21 11:47:45
【问题描述】:

我编写了一个函数来填充表单标签。该脚本创建选项,但它不会用我的数组条目填充选项标签。我希望你能帮助我。

这是我的代码

window.onload = function() {
    var VideoListe = $("VideoListe");
    for (i=0;i<Videos.length;i++) {
        var Entry = $("select").append('<option>');
       //Entry.text= Videos[i][1];
       $('<option>').append(Videos[i][0]);

        VideoListe.add(Entry ,null);
    }
}

编辑:这是该问题的 HTML 代码:

 <div class="wr_chapters">
    <form>
    <select id="VideoListe">
    </select>
    <button onclick="nextClip()">Zum nächsten Video</button>
  </form>

  </div>

【问题讨论】:

  • 首先var VideoListe = $("VideoListe");你一定忘记了#.在选择器前面的标志
  • @Muuta 你也可以粘贴一些 html。不清楚 VideoListe 是 css 类还是 id。
  • 对不起,我忘记了 html 代码块。 VideoListe 是一个 ID
  • @Muuta 我已经解决了你最后一个删除的问题,这里是 fiddle

标签: jquery arrays forms


【解决方案1】:

你可以这样做:

window.onload = function () {

    // Get the VideoListe element first with proper ID or class
    var VideoListe = $("#VideoListe");

    // Cache the select element here
    var Entry = $("select");
    for (i = 0; i < Videos.length; i++) {

        // Create a dynamic option with array data
        var $option = $('<option/>', {
            text: Videos[i][0],
            value: Videos[i][0]
        });

        // Append the option to the select list
        Entry.append($option);
    }

    VideoListe.add(Entry, null);
}

【讨论】:

    【解决方案2】:

    试试这个,

    $(document).ready(function(){
    
    var VideoListe = $("#VideoListe"); //or class not sure abt that
    if(VideoListe .length > 0) {
    var chunks = '<select>';
       for (i=0;i<Videos.length;i++)
       {
        chunks +='<option>'+Videos[i][0]+'</option>';
    
       }
    chunks +='</select>';
      //now chunks will be a select element
    }
    
    });
    

    【讨论】:

    • 您包含了问题评论中提到的错误:$("VideoListe");
    • 哎呀对不起@TJ。
    • 另外请您解释一下您的代码和 OPs 代码之间的区别是什么,以便他/她了解哪里出了问题。因为这只是一个代码 sn-p。
    猜你喜欢
    • 2019-03-31
    • 2014-03-18
    • 1970-01-01
    • 2014-01-04
    • 2018-03-19
    • 1970-01-01
    • 1970-01-01
    • 2015-08-11
    • 2013-09-27
    相关资源
    最近更新 更多