如何在Javascript中选择创建多个选项

问题描述 投票:-3回答:1

我可以单独选择选择列表但不能创建多个选项。

这是什么原因以及我该怎么办?

JS:

$.ajax({
    type: "GET",
    url: "core/spine.php",
    data: [...]
    cache: false,
    success: function (data) {

    data = JSON.parse(data);
    $.each(data.response, function(key, value) {
    $('#orderMultipleList')
    .append($("<option></option>")
    .attr("value",value)
            .text(value));
    });

HTML:

<select name="orderMultipleList[]" class="form-control"
multiple required></select>

JSON数据响应:

{"response":["2005","2006","2007","2008","2009","2010","2011","2012","2013","2014","2015","2016","2017","2018"]}

结果:

没有。

与工作代码:

<select name="orderMultipleList" class="form-control"
required></select>

与不工作代码:

<select name="orderMultipleList[]" class="form-control"
multiple required></select>


EDIT: Some friends don't understand. This code don't work with more than one choice. If I only make single choices, code is working but does not work when more than one selective. This has nothing to do with PHP
javascript jquery html append
1个回答
1
投票

你需要像这样添加一个id

<select name="orderMultipleList[]"  id = "orderMultipleList" class="form-control"
multiple required></select>

因为

$('#orderMultipleList')

需要id,而不是名字

© www.soinside.com 2019 - 2024. All rights reserved.