Comment utiliser le plus proche pour définir la valeur de la zone de texte à partir de la liste déroulante dans les lignes créées dynamiquement


Sens

Lorsque l'utilisateur sélectionne le nom du canal dans la liste déroulante, je souhaite définir son type de canal dans la zone de texte de la ligne créée dynamiquement. Je crée plusieurs lignes sur l'événement addRow. Ça marche bien

C'est du code html

 <table id="dataTable"  border="1" class="table table-striped table-bordered" >


            <tr>
                    <td><INPUT type="checkbox" name="chk[]"  data-parsley-errors-container="#checkbox-errors" /></td>
                    <td>
                            <SELECT name="channel_name[]" onclick ="get_type(this)"; class='channelname'>
                                    <option value="">Select...</option>
                                  <?php foreach($channel_list as $row) {
                                            $channelid = $row['channelid'];
                                            $channelname = $row['channelname'];?>
                                    <OPTION value='<?php echo $channelid ?>'><?php echo $channelname?></OPTION>

                            <?php } ?>
                            </SELECT>
                    </td>
                    <td><INPUT type="text" name="type[]" class="channeltype"/></td>
                     <td><INPUT type="text" name="partner_share[]"/></td>
            </tr>

    </table>

Code Javascript:

function get_type()
{
    $(".channelname").live("change", function() {

            var channel_id = $(this).find("option:selected").attr("value");
            $.ajax({
                    type: "POST",
                    url: '<?php echo base_url(); ?>index.php/partner/get_channel_type',
                    data: 'channelid='+channel_id,
                    async:   false
                     }).done(function( data1 ) {

                    if(data1){
                            alert(data1);
                            //$(this).closest('tr').children('td.type').val(data1);
                            //$(this).closest('tr').find('.type').val(data1);
                            $(this).closest("tr").find('input[name="channeltype[]"]').val(data1);
                            //$(this).closest("tr").find('input[name="usage_unit[]"]').val(ui.item.usage_unit);


                    }else{
                            alert("Channel type is not defined");

                    }


            });
    });
}
Zakaria Acharki

Il n'y a pas d'entrée avec le nom de channeltype[]la ligne:

$(this).closest("tr").find('input[name="channeltype[]"]').val(data1);

Devrait être :

$(this).closest("tr").find('input[name="type[]"]').val(data1);

Parce que le nom d'entrée du type de canal est type[]comme indiqué dans:

<td><INPUT type="text" name="type[]" class="channeltype"/></td>

Vous devez enregistrer l'instance jquery $(this)car elle sera différente dans le rappel de réussite:

 ...
 var channel_id = $(this).find("option:selected").attr("value");
 var _this = $(this); //Save current object

 $.ajax({
 ...

Ensuite, utilisez-le dans votre rappel:

...
_this.closest("tr").find('input[name="type[]"]').val(data1);
...

J'espère que cela t'aides.

Articles connexes