Retrieve from the database the "data-*value" attribute of a field through jquery

Asked

Viewed 536 times

0

How to recover a field’s "date" attribute from the database via jquery.

Today the system recovers the value(value) of the field, but now I also need to recover the date value of this field, as you ask (click here).

The purpose of this, is to make a comparison between the

value and . date-

In order to be able to make a condition to enable the save button, if any field of the form is changed and disable the button if nothing is changed in the form.

Follows as this the field in Html and Jquery:

<label for="descricao">Descrição</label>
    <input class="span12" id="descricaoEditar" type="text" name="descricao"  />
    <input id="urlAtualEditar" type="hidden" name="urlAtual" value=""  />

    $(document).on('click', '.editar', function(event) {
      $("#idEditar").val($(this).attr('idLancamento'));
      $("#descricaoEditar").val($(this).attr('descricao'));
      $("#descricaoEditar").val($(this).data('descricao'));
      $("#fornecedorEditar").val($(this).attr('cliente'));
      $("#valorEditar").val($(this).attr('valor'));
      $("#vencimentoEditar").val($(this).attr('vencimento'));
      $("#pagamentoEditar").val($(this).attr('pagamento'));
      $("#formaPgtoEditar").val($(this).attr('formaPgto'));
      $("#categoria_idEditar").val($(this).attr('categoria_id'));
      $("#conta_idEditar").val($(this).attr('conta_id'));
      $("#urlAtualEditar").val($(location).attr('href'));
      var estornado = $(this).attr('estornado');
      if(estornado == 1){
        $("#estornadoEditar").attr('checked', true);        
      }
      else{
        $("#estornadoEditar").attr('checked', false);
      }

      var baixado = $(this).attr('baixado');
      if(baixado == 1){
        $("#pagoEditar").attr('checked', true);
        $("#pagoEditar").attr('disabled', true);
        $("#estornadoEditar").attr('disabled', false);
        $("#divPagamentoEditar").show();
      }
      else{
        $("#pagoEditar").attr('checked', false); 
        $("#pagoEditar").attr('disabled', false);
        $("#estornadoEditar").attr('disabled', true);
        $("#divPagamentoEditar").hide();
      }     

3 answers

0

I think enable the button with "on change" would be enough, but below follows a more elaborate form, in which if you return the initial value of the field, the button is disabled.

$('form input').each(function() {
  $(this).data('value', $(this).val());
}).on('keyup', function() {
  $('button').prop('disabled', true);
  $('form input').each(function() {
    if ($(this).val() != $(this).data('value')) $('button').prop('disabled', false);
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form>
  <input value="valor1">
  <input value="valor2">
  <input value="valor3">
  <button disabled>Salvar</button>
</form>

0

To set the value $('seuelemento').attr('data-valor', valorquevcker);

To read the value $('seuelemento').attr('data-valor');


To take the value of the database in PHP and play in the view in your input would be something +- like this:

<input type="text" data-valor="<?php print $variavelcomovalordobanco ?>" />

Maybe the way to print the variable is not right, I’m not good in php, but it is something like that. You will have to make PHP print the value inside the input, because the value that you Ker comes from the database

  • Renan, E to read the value saved in the database?

  • @Wagnerson to read from the database you will have to use your backend language (php, nodejs, c#, java, etc...). jquery does not do this, you will expose this database data with its direct backend language in the input element If it is PHP: When rendering the input, put the data-value="<attribute? php $variavelcomovalordobanco ? >" After that, by jquery vc will be able to get the data

0

You can pick through

var valor     = $("#estornadoEditar").val();
var descricao = $("#estornadoEditar").data("descricao");
  • It didn’t work. $("#descricaoEditar").val($(this).attr('descricao')); This function. I would like to know how I assign this same value to data-field. ;<input type="text" value="valor recuperado do banco" data-valor="valor recuperado do banco" />

  • $("#descricationEdit"). date('value', 'value recovered from the bank');

  • And how can I get the value recorded in the Description column, saved in the databank?

  • I don’t think your question is clear, Wagner. I thought your doubt was in manipulating the data-value attribute, not in finding a value in the database. Can clarify what your real doubt is?

  • So Vinicius, would search the database for the date attribute-. I know how to recover the value, but I don’t know how to assign this same value to date-.

Browser other questions tagged

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