update input with jQuery

Asked

Viewed 882 times

0

In each table row I have an input, I need to change the input value with the row position value. How can I do this?

$(document).ready(function () {
        $('tbody').sortable({
            axis: 'y',
            containment: 'parent',
            change: function(){ 
console.log("change"); 
              $.each($(".tab_dados tbody>tr"), function(indice, obj){
                 $(this).find($("td>input[type=text]")).val(indice + 1);
              });
        }
        });
    });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.11.4/jquery-ui.js"></script>
<table class="tab_dados">
  <thead>
  <tr>
    <th style="width: 30px;"></th>
    <th>CÓDIGO</th>
    <th>NOME</th>
    <th>input</th>
  </tr>
  </thead>
  <tbody>
  <tr>
    <td>&nbsp;</td>
    <td>25</td>
    <td>HUGO</td>
    <td><input type="text" name="id25" value="1"></td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td>26</td>
    <td>bruno</td>
    <td><input type="text" name="id26" value="2"></td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td>27</td>
    <td>rafael</td>
    <td><input type="text" name="id27" value="3"></td>
  </tr>
</tbody>
</table>

1 answer

3


   $(document).ready(function () {
        $('tbody').sortable({
            axis: 'y',
            containment: 'parent',
            animation: 200,
            stop: function(){  
                  $.each($(".tab_dados tbody>tr"), function(indice, obj){
                     $(this).find($("input[type=text]")).val(indice + 1);
                  });
            }
        });
    });

At the event: stop sortable can check all lines.

Take a look at the documentation for what you’ll need from now on: Sortable

  • I didn’t quite understand. could you explain me better.

  • It was clearer, @Hugoborges?

  • I had tried this and it didn’t work. I even edited the question with your answer. It didn’t log tbm errors.

  • If you debug js, it stops in the change function?

  • The change function is running, I put a log in to check. The thing that does not occur is the change of the input values. Since they always have to be in order. Ie the 1st input will always be 1, the 2nd will always be 2 and etc...

  • Just to test, put the type="text" in the inpt. And also change here: $(this). find($("td>input[type=text]"). val(Indice + 1);

  • Ready changed, I put the log for you see tbm. Still not working.

  • Ready. Just take a look at the documentation for what you’ll need from now on. (=

  • worked out, thank you very much

Show 4 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.