简体   繁体   English

使用 jquery 将数据从 ajax 附加到 html select 标签

[英]Appending data from ajax to html select tag using jquery

How can I append the data from API to a select tag in html.如何将来自API的数据附加到 html 中的选择标记。

This is my javascript code.这是我的javascript代码。

 var options = '';
 for (var i = 0; i < data.total; i++) {
      options += '<option value="' + data.data[i].projectName + '">' + data.data[i].projectName + '</option>';
}
$("#selectProjectName").append(options);

This is my html code.这是我的html代码。

<select id="selectProjectName" class="form-control show-tick selectpicker" title="Choose project name">

</select>

The data is shown in the console of the browser, but it is not appended to the select tag while hard coded values are shown in the select tag.数据显示在浏览器的控制台中,但它不会附加到 select 标记中,而硬编码值显示在 select 标记中。

Using AdminBSBMaterialDesign-master template.使用AdminBSBMaterialDesign-master模板。

It looks that you're using selectpicker, so after you change anything in the select element you need to refresh it using $(".selectpicker").selectpicker("refresh"); 看起来您正在使用selectpicker,因此在select元素中进行任何更改之后,都需要使用$(“。selectpicker”)。selectpicker(“ refresh”);对其进行刷新。

This is in the documentation here . 这在此处的文档中。

Also, there's nothing apparently wrong with your method of appending it, as long as data.total returns the length of it (otherwise, use .length ) but just as a FYI you can use the following syntax: 另外,只要data.total返回其长度(否则,请使用.length ),添加它的方法就没有任何问题,但就像供您参考,您可以使用以下语法:

$('#select').append($('<option>', {value:1, text:'One'}));

To make things easier and nicer for you. 使事情变得更轻松,更好。

Cheers! 干杯! :) :)

I assume your data is key value type 我假设您的数据是键值类型

 var newOptions = { 'red' : 'Red', 'blue' : 'Blue', 'green' : 'Green', 'yellow' : 'Yellow' }; // get this data from server var selectedOption = 'green'; var select = $('#selectProjectName'); var options = select.prop('options'); $.each(newOptions, function(val, text) { options[options.length] = new Option(text, val); }); select.val(selectedOption); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <select id="selectProjectName" class="form-control show-tick selectpicker" title="Choose project name"> </select> 

Try .length instead of .total 尝试使用.length而不是.total

var options = '';

for (var i = 0; i < data.length; i++) {
 options += '<option value="' + data[i].projectName + '">' + data[i].projectName + '</option>';
}

$("#selectProjectName").append(options);
$("#selectProjectName").selectpicker("refresh");

Instead of using string concatenation, you can use jquery style of creating option element and appending the same as below. 您可以使用jquery样式创建option元素并附加如下内容,而不是使用字符串连接。

Please check whether your data structure is similar to the one below according to the code that you are trying. 请根据您尝试的代码,检查您的数据结构是否与下面的数据结构相似。

Otherwise better to use data.data.length instead of data.total 否则最好使用data.data.length而不是data.total

 var data = { total: 3, data: [ { projectName: 'jquery' }, { projectName: 'reactjs' }, { projectName: 'angular' } ] }; for (var i = 0; i < data.data.length; i++) { var option = $('<option>', { value: data.data[i].projectName, html: data.data[i].projectName }); $("#selectProjectName").append(option); } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <select id="selectProjectName" class="form-control show-tick selectpicker" title="Choose project name"> </select> 

Use .length instead of .total Inside the loop, create each option element and add them one by one to the select element: 在循环中使用.length而不是.total,创建每个option元素并将它们一个一个地添加到select元素中:

    for (var i = 0; i < data.length; i++) {
        var opt = $('<option></option>');
        opt.attr('value', data.data[i].projectName).text(data.data[i].projectName);
        $("#selectProjectName").append(opt);
    }

I found I was missing...我发现我不见了……

<script   src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

after I added it fixed the problem.我添加后解决了问题。

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

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