Пустые данные в html-таблице дополнений

0

У меня есть следующий скрипт, который заполняет таблицу данными при выборе выпадающего значения:

<script>
     $(document).ready(function(){

     $('#commodity_name').change(function (){
         //picks the value of the selected commodity_name from the dropdown
         option = $(this).find('option:selected').val();
         html='';
         htmlhead='';
        // alert(option)

         $.ajax({
             type:"GET",
             //gets data from the url specified based on the selected commodity_name in the dropdown
             url:"<?php echo base_url();?>user_transactions/return_details/"+option,
             dataType:"json",
             success:function(data){
                for(i=0;i<data.length;i++){

                //Loops through the data to give out the data in a table format
                    //alert(data[i].transaction_type)
         html += '<tr>\n\
        <td><input type="text" id="commodity_name' + i + '" name="commodity_name[]" value="'+data[i].commodity_name+'"/></td>\n\
        <td><input type="text" id="transaction_type' + i + '" name="transaction_type[]" value="'+data[i].transaction_type+'"/></td>\n\
        <td><input type="text" id="total_quantity' + i + '" name="total_quantity[]" value="'+data[i].total_quantity+'"/></td>\n\
        <td><input type="text" id="remarks' + i + '" name="remarks[]" value="'+data[i].remarks+'"/></td>\n\
        <td><input type="text" id="unit_cost' + i + '" name="unit_cost[]" value="'+data[i].unit_cost+'"/></td>\n\
        <td></td><td></td></tr> ';
        }
        htmlhead+='\n\
             <th>Commodity Name</th>\n\
             <th>Transaction Type</th> \n\
            <th>Available  Quantity</th>         \n\
            <th>Available  Quantity</th> \n\
            <th>Department</th>\n\
            <th>Requestor Name</th>\n\
            ';
                                //alert(html);
                                //alert(htmlhead);
       $('#thead').append(htmlhead);
        $('#you').append(html);
        //delegated submit handlers for the forms inside the table
$('#issue_1').on('click', function (e) {
    e.preventDefault();

    //read the form data ans submit it to someurl
    $.post('<?php echo base_url()?>transactions/issues', $('#Issues_Form').serialize(), function () {
        //success do something
        alert("Success");
        var url = "<?php echo base_url()?>transactions/view_request";    
        $(location).attr('href',url);
    }).fail(function () {
        //error do something
        alert("Failed please try again later or contact the system Administrator");
    })
})

             },
             error:function(data){

             }
         })

     });
     });



</script>

Как я могу очистить данные из таблицы html, которая была добавлена, когда я выбираю следующее товарное имя в раскрывающемся списке, я получаю только данные, возвращенные из нового выбора, выбранного из выпадающего списка.

  • 0
    Просто замечание: когда я вижу людей, использующих «alert» для отладки, мне подсказывают, что они не знают о console.log
Теги:

2 ответа

0
Лучший ответ

Просто выпустите html перед добавлением.

...
$('#thead').empty();
$('#you').empty();

$('#thead').append(htmlhead);
$('#you').append(html);
...
0

Попробуйте использовать метод.detach() JQuery. Здесь ссылка: http://api.jquery.com/detach/

Ещё вопросы

Сообщество Overcoder
Наверх
Меню