How do I make an IF and disable a field using script

2

I would like some help, wanted to make an IF to disable or enable a field that comes with a value of 1 or 0.

$(document).ready(function () {
  $("#btnProduto").click(function () {
    $.ajax({
      type: "POST",
      url: "Selecionar",
      data: { id_produto: $("#ListaProdutos").val() },
      success: function (mensagem) {
        //alert(mensaje);
        //$("#idproducto").val(mensaje.Nombre);
        $(mensagem).each(function (index, item) {
          //recibir datos json
          $("#descricao").val(item.descricao),
          $("#id_produto").val(item.id_produto)
          $("#pvenda").val(item.pvenda)
        });
      }
    });
  });
});

My field I want to block or unblock is $("#pvenda").val(item.pvenda) , this field will only return me 2 values, 1 or 0, if it is 0 I want to disable the field so the user does not edit it, and if it is 1 I want that the field is enabled for the user informs the value you want. Thank you!

    
asked by anonymous 26.08.2017 / 15:24

1 answer

2

Use the disabled property for this, like this:

if ( $("#pvenda").val(item.pvenda).val() == 0 )
{
   $("#pvenda").val(item.pvenda).prop("disabled", true)
}
    
26.08.2017 / 15:32