简体   繁体   English

向多个选择元素添加新选项问题

[英]Adding new options to multiple select elements issue

I have a form where users can pick several locations they want to stay at and they have to specify the time they are going to be at each location but first, they have to enter how many days in general they are going to be at every location, for instance, if I want to be at two locations 2 days at the first one and 3 at the second one, I have to enter 5 days first and then pick the locations I want to stay at specifying for each location how many days I want to be there. 我有一个表格,用户可以选择几个想要停留的地点,他们必须指定要在每个地点停留的时间,但首先,他们必须输入每个地点通常要停留多少天,例如,如果我想在两个地点的第一个地点停留2天,在第二个地点停留3天,则必须先输入5天,然后选择要停留的地点,为每个地点指定我要停留多少天想要在那里。 Well the thing is that to prevent the user to enter a wrong number of days for a location I created a select elemento for each location so when the user enters the general number of days I fill up the select elements with that information (options from 1 to day number), that is working just fine, but when the user picks a location and especify a day amount for that location I want the rest of the select elements to be filled with the ramaining days, so I use this code to do that 好是为了防止用户为某个位置输入错误的天数,我为每个位置创建了一个选择元素,因此当用户输入一般天数时,我用该信息填充了选择元素(从1开始的选项到天数),这很好,但是当用户选择一个位置并指定该位置的天数时,我希望其余的选择元素都填充剩余的天数,因此我使用此代码来执行此操作

function fillUpSelects2(start, top, who) {

    var optionArray = new Array();

    // create the new options
    for (var i = 1; i <= top; i++) {
        optionArray.push(new Option(i, i));
    }

    // get all the select elements
    var selects = jQuery("select.estanciaselect");

    // loop through the selects to change their content
    for (var i = 0; i < selects.length; i++) {

        // if selects[i] is not the one who triggered the change event
        if (selects[i].getAttribute('id') != who) {

            // then I erase all its options
            selects[i].options.length = 0;

            //and here is where it is supposed to add the new options
            for (var j = 0; j < optionArray.length; j++) {
               selects[i].options[selects[i].options.length] = optionArray[j];
            }
        }
    }
}

well when I run this all the selects end up empty but the last one which gets filled up properlly 好吧,当我运行此选项时,所有选择最终都为空,但最后一个被正确填充

If you're using jQuery, you might as well use it to append them: 如果您使用的是jQuery,则最好使用它来附加它们:

for (var i = 1; i <= top; i++) {
    $(".selects").prepend($("<option>")
                              .val(i)       // Might have to use .attr("value", i) instead
                              .html(i));
}

Here's a jsFiddle demonstrating two options you could use (including the one above), but there are plenty more: 这是一个jsFiddle,展示了可以使用的两个选项(包括上面的一个),但是还有更多选择:

http://jsfiddle.net/tRHYk/ http://jsfiddle.net/tRHYk/

Following is an approach on how to add items to multi-select (though not a direct answer to your question) 以下是有关如何添加项目以进行多选的方法(尽管不是您问题的直接答案)

<script src="http://ajax.aspnetcdn.com/ajax/jquery/jquery-1.9.0.js"></script>
<script type="text/javascript">
    $(document).ready(function () {

        var o = new Option("option text", "value");
        $(o).html("option text");
        $("#leftValues").append(o);

    });
</script>

<select id="leftValues" size="5" multiple></select>

Refer Adding options to a <select> using jQuery? 请参阅使用jQuery向<select>添加选项? too

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

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