N
N
Nikita Ronzin2017-03-29 12:13:31
JavaScript
Nikita Ronzin, 2017-03-29 12:13:31

How to pass the record ID to the handler?

I need to pass an ID value which is marked as $phone->primaryKey

<?php foreach ($model->phones as $phone) : ?>
        <div class="row" id="phone_<?php echo $phone->primaryKey ?>">
            <?php
            echo $form->hiddenField($phone, 'Type_ID[' . $phone->primaryKey . ']', array('value' => $phone->Type_ID));
            echo $form->hiddenField($phone, 'ID[' . $phone->primaryKey . ']', array('value' => $phone->ID));
            ?>
            <?php echo $phone->type->Type_Phone; ?><br/>.
            <?php echo $form->textField($phone, 'Number[' . $phone->primaryKey . ']', array('value' => $phone->Number, 'rows' => 1, 'cols' => 40)); ?>
            <?php $this->widget('booster.widgets.TbButton', array(
                'buttonType' => 'link',
                'context' => 'default',
                'icon' => 'glyphicon glyphicon-trash',
                'url' => array('phone/delete', 'id' => $phone->primaryKey),
                'htmlOptions' => array(
                    'class' => 'phone-remove',
                    'data-toggle' => "tooltip",
                    'title' => 'Удалить',
                   )
            )); ?>
        </div>

and pass it to the handler
jQuery(document).ready(function () {
    jQuery('.phone-remove').on('click', function () {
        if (!confirm('Вы уверены, что хотите удалить данный элемент?')) return false;
        var id=''; //тут надо как-то получить id
        jQuery.ajax({
            type: 'POST',
            url: jQuery(this).attr('href'),
            dataType:'json',
            success: function (data) {
                if (data.type==='success'){
                    $('#phone_' + id).detach();
                }
            }
        });
        return false;
    });
});

in the var id field, I can't figure out how to correctly specify its value in html

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
Roman Frank, 2017-03-29
@Ababinili

In htmlOptions add
A in js
var id = $('.phone-remove').data().id;

A
Andrey Tokmakov, 2017-03-29
@NPC

ruseller.com/lessons.php?rub=29&id=1030
dimation.ru/jquery/499-get-url-parameters-and-valu...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question