简体   繁体   English

使用javascript填充选择框

[英]Populate a Select Box using javascript

I've looked everywhere, but I can't find a solution to this problem. 我到处都看过,但是找不到这个问题的解决方案。 I've spent hours debugging, and I can't figure it how myself either. 我花了数小时进行调试,但我自己也无法弄清楚。 I'm sorry if this is an easy fix, I just can't seem to get it. 很抱歉,如果这是一个简单的解决方法,我似乎无法理解。

I have an empty select box as shown here: 我有一个空的选择框,如下所示:

<p id='draftChoice' class='hide'>
    Select draft choice:
    <select id='draftList'>
    </select>
    <input type=submit value='Draft' id='draft'>
</p>

When you click draft, this will run: 当您单击草稿时,它将运行:

draftRound = function() {
var draftee = document.getElementById('#draftList').value;
prospectPool.splice(draftee, 1);
prospectPool.players.splice(0, 13);
};

However, I get an error: "Uncaught TypeError: Cannot read property 'value' of null" on this line: 但是,我在此行收到错误:“未捕获的TypeError:无法读取null的属性'value'”:

var draftee = document.getElementById('#draftList').value;

My best guess is that it the value isn't being properly defined when I populate the box, which I do here: 我最好的猜测是,当我填充此框时,该值未正确定义,我在这里执行以下操作:

createDraftList = function() {
//$('#right').html("");
//$('#draftList').html("");
for (var i = 0; i < prospectPool.players.length; i++) {
    $('#right').append("<h4>" + prospectPool.players[i].name + "</h4><div><p>" + prospectPool.players[i].overall + "<br>" + prospectPool.players[i].position + "<br>" + prospectPool.players[i].playerType);
}
$('#right').accordion();
$('#draftChoice').removeClass('hide');
    $(prospectPool.players).each(function() {
        $('#draftList').append($("<option>").attr('value',this.name).text(this.name))  + "</option>";
    });
 };

As a follow up question, I want to have multiple 'rounds' of this draft. 作为后续问题,我想对该草案进行多个“回合”。 When I do, I want to update the select box, as well as the accordion I have on the screen (populated in the last block of text). 完成后,我想更新选择框以及屏幕上显示的手风琴(填充在最后一块文本中)。 My current method used the two commented out lines. 我当前的方法使用了两条注释行。 When I do this, the correct text shows up, but I lost my accordion formatting, and I have no idea why. 当我这样做时,会显示正确的文本,但是我丢失了手风琴的格式,也不知道为什么。 Anybody who can help, I greatly appreciate it! 任何可以帮助的人,我将不胜感激!

Change 更改

document.getElementById('#draftList')

to

document.getElementById('draftList')

The # is only used in CSS. #仅在CSS中使用。 When you're grabbing an element by ID, it only needs to actual ID. 当您通过ID抓取元素时,只需要使用实际ID即可。

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

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