1
I have some items returned from the bank that is listed in a table. To add an item, I call a modal where I fill in the fields by saving them in the database. However, if filling in this field is wrong, I need to edit it. The point is, how do I redeem the values for each line by clicking on edit
to make a update at the bank?
See the code below as an example:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="http://getbootstrap.com/dist/js/bootstrap.js"></script>
<link href="http://getbootstrap.com/dist/css/bootstrap.css" rel="stylesheet"/>
<table border="1" cellspacing="1" cellpadding="5">
<thead>
<tr>
<th width="6%">Data</th>
<th width="40%">Descrição</th>
<th width="10%">Ação</th>
</tr>
</thead>
<tbody>
<tr>
<td rel="date">27/10/2016</td>
<td rel="description">Febre amarela</td>
<td rel="action">
<a data-toggle="modal" href="http://fiddle.jshell.net/bHmRB/51/show/" data-target="#myModal">Edit</a>
<a data-toggle="modal" href="#" data-target="#add">Delete</a>
</td>
</tr>
<tr>
<td rel="date">27/10/2016</td>
<td rel="description">Gripe suína</td>
<td rel="action">
<a data-toggle="modal" href="http://fiddle.jshell.net/bHmRB/51/show/" data-target="#myModal">Edit</a>
<a data-toggle="modal" href="#" data-target="#add">Delete</a>
</td>
</tr>
</tbody>
</table>
<!-- Modal -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h4 class="modal-title">Modal title</h4>
</div>
<div class="modal-body">
<form>
Descrição: <input type="text"/>
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Fechar</button>
<button type="button" class="btn btn-primary">Salvar</button>
</div>
</div>
<!-- /.modal-content -->
</div>
<!-- /.modal-dialog -->
</div>
<!-- /.modal -->
Note: The delete button is only dummy.
EDIT
I’m using PHP as back end to search this data in the database.
If you have other fields, it would just replicate by changing the rel?
– viana
For example, if you open the same modal to both edit to add, as I do to grab the value of the button I’m clicking (whether it’s Edit or add)?
– viana
Yes, for other fields, just change the selector and save to different variables. In your example is Description, but can be n variables..
– BrTkCa
To see which button you are clicking you can do so:
$(this).html() == 'Edit')
. Placing inside aif
to check if it is Edit or add.$(this)
is the selector that gets the object of the element that was target @Acklay– BrTkCa
Just one more question. How do I grab instead of html, get value?
– viana
Value is usually for inputs, using . val(). For other elements, if I’m not mistaken . attr('value') or . prop('value')
– BrTkCa
worked with attr. thanks. abs.
– viana
It would be easier to use
.parents('tr')
instead of.parent().parent()
. This will raise to the relativetr
, or you can use the.closest('tr')
. Both work, anyway. ;)– Inkeliz
I didn’t know the method
parents
@Inkeliz, I learned one more =] thank you very much!– BrTkCa