1

I have the code that dynamically generates textboxes and select boxes upon a button click. I want to fetch the data from DB and display in the dynamically generated select box.

Fiddle http://jsfiddle.net/hEByw/11/ shows how the text and selectboxes are generated dynamically.

I have tried the following part of code to fetch the data from DB and Put into dynamically generated select box(Tax Type) but its not working for me.

//To Display the tax types from DB
$(function(){
    var items="";
    $.getJSON("get_tax_type.php",function(data){
        $.each(data,function(index,item) 
        {
            items+="<option value='"+item.id+"'>"+item.name+"</option>";
        });
        $("#tax_type' + counter + '").html(items); 
    });
});

I feel the way I am doing is wrong.Can anybody suggest where am I doing wrong or the proper way of implementing it. I am newbie in jquery. Any help is appreciated.Thanks in advance.

PHP Code(get_tax_type.php)

    <?php 
    include('includes/db.php');
    $q = "select TaxID, TaxName from tax";
    $sql = mysql_query($q);
    $data = array();
    while($row = mysql_fetch_array($sql, true)){
    $data[] = $row; 
    };
    echo json_encode($data);
?>
4

1 回答 1

1

尝试这个。更新

    $(document).ready(function() {

        var items = "";
        $.getJSON("get_tax_type.php", function(data) {
            alert(data);
            $.each(data, function(index, item) {
                $("#tax_type" + parseInt(index) + parseInt(1)).empty();
                $("#tax_type" + parseInt(index) + parseInt(1)).append("<option value='" + item.TaxID+ "'>" + item.TaxName+ "</option>");
            });

        }, 'json');

    });
于 2013-05-27T11:31:23.220 回答