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

Asked

Viewed 24 times

2

I’d like some help, wanted to do an IF to disable or enable a field that comes with the value 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 that I want to block or unlock is the $("#pvenda").val(item.pvenda), this field will return me only 2 values, 1 or 0, if it is 0 I want to disable the field for the user not edit it, and if it is 1 I want the field to be enabled for the user to inform the value you want. Thanks in advance !!

  • Alysson you are getting the values right in ajax?

  • The weird thing is you get $("#pvenda").val(item.pvenda), a JSON does not offer data that way.

  • to @Leonardobonetti, ta bringing the right value

1 answer

2


Use the property disabled for that, so:

if ( $("#pvenda").val(item.pvenda).val() == 0 )
{
   $("#pvenda").val(item.pvenda).prop("disabled", true)
}

Browser other questions tagged

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