我们应该如何在下拉列表中自动显示所选内容?

时间:2018-06-01 05:48:40

标签: ajax codeigniter

我有一个文本框和两个下拉列表。当我在文本框中提供代码时,它应该自动在下拉列表中显示类别名称和product_name,它应该在下拉列表中自动显示为选中状态?任何人都可以帮我代码吗?到目前为止,我通过javascript传递了我的代码:

视图:

<td style='width:7%'><input type='text' name='code[]' id='code_1' class="form-control" style='width:100%' onChange="getdetailsbycode(this.value);"/>
                </td>
                <td style='width:20%' id='producttd_1'><?php //print_r($prolist); ?><select name='item[]' id='item_1' class="form-control selectpicker"  style='width:100%'  data-show-subtext="true" data-live-search="true" >
                    <?php
                    if(!empty($prolist))
                    {
                    //print_r($prolist);
                        foreach($prolist as $pro)
                        {
                            $opn1.="<option value='".$pro['id']."'>".$pro['product_name']."</option>";


                        }   
                        echo $opn1;

                    }
                    ?>
<td style='width:20%' id='categorytd_1'><select name='category[]' id='category_1' class="form-control selectpicker  categorydropdown"  data-show-subtext="true" data-live-search="true" onChange='show_product(this.value,1);' ><option>--Select--</option>

                <?php 
             if(!empty($category_list))
            {
                foreach($category_list as $cat)
                {
                     $opn2.="<option value='".$cat['id']."'>".$cat['category_name']."</option>";
                }
                echo $opn2;
            }
              ?>

<script>
function getdetailsbycode(code)
{

    $.ajax({
        url: '<?php  echo base_url();?>sales/getprobycode',
        data: ({"code":code}),
        dataType: 'html', 
        type: 'post',
        success: function(data) {

         data=data.split("|");
           alert(data);

            document.getElementById("producttd_1").innerHTML=data[0];

            document.getElementById("categorytd_1").innerHTML=data[1];




        }            
    });

}
</script>

我的控制器:

 function getprobycode()
    {
        $code=$this->input->post('code');
        $opn1='<input type="text" name="code[]" id="code_'.$code.'" class="form-control"  />';
        $opn1='<select name="item[]" id="item_'.$code.'" class="form-control selectpicker" data-show-subtext="true" data-live-search="true">';
        if($code!='')
        {

            $opn1.='<option value="">--Select--</option>';
            $prolist=$this->sale_model->getproductbycode($code);
                //print_r($prolist);
            if(!empty($prolist))
            {
            //print_r($prolist);
                foreach($prolist as $pro)
                {
                    $opn1.="<option value='".$pro['category_id']."'>".$pro['product_name']."</option>";

                }
            }
        }


        $opn2='<input type="text" name="code[]" id="code_'.$code.'" class="form-control"  />';
        $opn2='<select name="category[]" id="category_'.$code.'" class="form-control selectpicker"  data-show-subtext="true" data-live-search="true" >';

        if($code!='')
        {
        $opn2.='<option value="">--Select--</option>';
            $catlist=$this->sale_model->getcategorybyid($code);

            if(!empty($catlist))
            {
                foreach($catlist as $cat)
                {
                    $opn2.="<option value='".$cat['id']."'>".$cat['category_name']."</option>";
                }
            }


        }

            echo $opn1."|".$opn2;

    }

我的模特:

function getcategorybyid($code1)
    {
        $this->db->select('*');
        $this->db->from('category',$code1);
        return $this->db->get()->result_array();
    }
    function getproductbycode($code)
    {

            $this->db->select('*');
            $this->db->from('product',$code);
            return $this->db->get()->result_array();



    }

0 个答案:

没有答案