jQuery使用数组中的数据创建选择标签 [英] Jquery create select tag with data from array

查看:159
本文介绍了jQuery使用数组中的数据创建选择标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要访问数据库并更新选择标签的选项.我的代码在这里.

I need to access database and update options of a select tag. My code is here.

 $(window).load(function () {

     $.getJSON('http://localhost/ABC/web/app_dev.php/doctorFillOption', function (data) {

         var length = data.length,
             element = null;
         $('#idcatFill').append('<select id ="idCat"  style="width:200px;margin-left:30px;margin-top:5px;" type="text"><option value=""></option>');

         for (var i = 0; i < length; i++) {
             row = data[i];
             alert("id" + row['id'] + ' ' + row['category_name']);
             $('#idcatFill').append(
                 '<option value="' + row['id'] + '">' + row['category_name'] + '</option>'
             );
         }

     });
     $('#idcatFill').append('</select><br/>');
 });

但是它没有选项.需要帮忙.谢谢.

But it doesn't get options. Need help. Thanks.

推荐答案

使用以下代码附加标签:

Use the following code to append your tag:

$("#idcatFill").append(function() {
    var elem = $('<select id ="idCat"  style="width:200px;margin-left:30px;margin-top:5px;">');
    for (var i = 0; i < length; i++) {
         row = data[i];
         elem.append('<option value="' + row['id'] + '">' + row['category_name'] + '</option>');
    }
    return elem;
});

您不能将不完整的元素附加到dom.您需要先构建<select>标记,然后再插入它.

you can't attach incomplete elements to the dom. You need to build your <select> tag before inserting it.

这篇关于jQuery使用数组中的数据创建选择标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆