简体   繁体   English

使用JavaScript将选项添加到选择菜单时出错

[英]Error adding options to a select menu with javascript

I was having two problems: the first is that javascript is not adding options to a menu, and the second was an "Unexpected or invalid token" error at the end of my for loop. 我遇到了两个问题:第一个是javascript没有在菜单中添加选项,第二个是我的for循环结束时出现“意外或无效令牌”错误。

Update: The token error was a weird character thing. 更新:令牌错误是一个奇怪的字符。 I deleted the line that was causing the problem and retyped it and now I am not getting the error anymore, but my script still is not adding options. 我删除了引起问题的行,然后重新输入了该行,现在不再出现错误,但是我的脚本仍未添加选项。

I have read about adding options to a select menu but the answers I've found there haven't worked for me. 我已经读过有关在选择菜单中添加选项的信息,但是我在那里找到的答案对我没有用。 I should point out that this is part of a Joomla website (v3.8), because that can cause some unexpected behaviour. 我应该指出,这是Joomla网站(v3.8)的一部分,因为这可能会导致某些意外行为。

I have a function which is supposed to pick out a particular select menu based on the string "id", clear its contents, and re-populate it with the elements of the "options" array. 我有一个函数,该函数应该根据字符串“ id”选择一个特定的选择菜单,清除其内容,然后使用“ options”数组的元素重新填充它。

function resetSelectMenu(id, options){
    // Selects the correct menu, enables it, and removes all of its options. 
    var selectMenu = jQuery('#sel-' + id);
    selectMenu.prop('disabled', false);
    selectMenu.empty();        
    // Makes an option element and sets its text and value.
    var el = document.createElement("option");
    el.text = 'blah';
    el.value = 'blah';
    // Does not succeed in adding an option to menu.
    selectMenu.add(el);

    // I declared loop variables here to make sure there are no re-declaration issues.
    var i;
    var opt;
    // The loop is entered and the array is iterated through.
    for(i = 0; i < options.length; i++){
        opt = options[i];
        el = document.createElement("option");
        el.text = opt;
        el.value = i + 1;
        // Does not succeed in adding an option to menu.
        selectMenu.add(el);
    }​

}

The function does target the correct menu and clear its contents, but it is not adding the "blah" option or anything from the "options" array. 该函数确实以正确的菜单为目标并清除了其内容,但未添加“ blah”选项或“ options”数组中的任何内容。 I've tried using "appendChild(el)" instead of add but I get an error of the form "appendChild() is not a function". 我尝试使用“ appendChild(el)”代替“ add”,但出现“ appendChild()不是函数”形式的错误。 I did a lot of console logs to determine that all the parts of the code are working as expected except for "selectMenu.add(el);" 我做了很多控制台日志来确定代码的所有部分都按预期工作,除了“ selectMenu.add(el);”。

So this was just a noob mistake. 因此,这只是一个菜鸟错误。 I looked a bit harder and found this related question , and the top answer by Matt told me I needed to use append(el) instead of add(el) . 我看起来更加努力,发现了这个相关问题 ,Matt的最高答案告诉我,我需要使用append(el)而不是add(el) I looked into the documentation and the add method just affects the jQuery object but doesn't actually affect the DOM at all, whereas append does. 我查看了文档,并且add方法仅影响jQuery对象,但实际上完全不影响DOM,而append则影响。 If I were to use add I would need to explicitly tell the jQuery object to refresh the DOM. 如果要使用add ,则需要明确地告诉jQuery对象刷新DOM。

It's also possible that there is still an issue with me using selectMenu.empty() instead of something like selectMenu.find('option').remove() , but at the moment it's not causing any errors. 使用selectMenu.empty()而不是selectMenu.find('option').remove()类的东西对我来说仍然可能有问题,但目前还没有引起任何错误。 Since my select element only contains option elements I feel like these two commands would be the same, but on the other hand maybe the latter is better because it allows for the addition of different kinds of elements later. 由于我的select元素仅包含option元素,因此我觉得这两个命令将是相同的,但另一方面,后者可能更好,因为它允许稍后添加不同种类的元素。

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

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