简体   繁体   中英

How to get value from selected row in a modal?

When i select a row from a table, then open a modal with input fields which I can edit, but I am sending to query only first row data.

How I can get value from selected row? In modal inputs I get correct rows data.

$('.targetClass').submit(function(event) {
    event.preventDefault();
    $.ajax({
        type: 'POST',
        url: '/binitex_task/index/edit',
        data: {
            name: $('#name_id').val(),
            surname: $('#surname_id').val(),
            year_of_birth: $('#year_of_birth_id').val(),
            city_of_birth: $('#city_of_birth_id').val(),
            university: $('#university_id').val(),
            insurance_number: $('#insurance_number_id').val(),
            id: $('img').attr('id')
        },
        success: function() {}
    });
});

Modal with form where the input fields are, where is the value which I need to send to query to update a database table.

<div class="modal-body">
    <form method="POST" class="targetClass" id='target'>
        <input id='name_id' type="text" name="name" value="<?php echo $student['name']; ?>">
        <input id='surname_id' type="text" name="surname" value="<?php echo $student['surname']; ?>">
        <input id='year_of_birth_id' type="text" name="year_of_birth" value="<?php echo $student['year_of_birth']; ?>">
        <input id='city_of_birth_id' type="text" name="city_of_birth" value="<?php echo $student['city_of_birth']; ?>">
        <input id='university_id' type="text" name="university" value="<?php echo $student['university']; ?>">
        <input id='insurance_number_id' type="text" name="insurance_number" value="<?php echo $student['insurance_number']; ?>">
        <input id="saveBtn" name='save' type="submit">
    </form>
</div>

Do u mean that u have many rows of "name_id, surname_id, year_of_birth_id, etc..."??? (and u want to edit a specific row of the record)

That being the case, then the $('img').attr('id') should be the problem. If u call $('img'), u would be calling the first row that containing the "img". Also, u cant use "id=xxxx" if u have many rows of data. (reason is same as the $('img') problem)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM