How to disable an input field?

Asked

Viewed 2,646 times

0

After receiving a value, how to block an input field so that it does not have its value changed? below there are two fields; how to block only the field 'FANTASY NAME' and keep 'CNPJ' to receive a new value?

$(document).ready(function(){

  $('#pesquisar').on('click', function(e) {
    
   
    e.preventDefault();
    
   
    var cnpj = $('#cnpj').val().replace(/[^0-9]/g, '');
    
    
    if(cnpj.length == 14) {
    
     
      $.ajax({
        url:'https://www.receitaws.com.br/v1/cnpj/' + cnpj,
        method:'GET',
        dataType: 'jsonp',
        complete: function(xhr){
        
         
          response = xhr.responseJSON;
          
       
          if(response.status == 'OK') {
          
          
            $('#nome').val(response.fantasia);
            ;
          
          } else {
            alert(response.message);
          }
        }
      });
    
   
    } else {
      alert('CNPJ inválido');
    }
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form id="formu">
        <p>Cadastro de Empresa</p>
    		<fieldset id="first">
    
    			<label for="cnpj">CNPJ</label><br/>
    				<input id="cnpj" type="text" name="cnpj" size="35" maxlength="14" style="margin-right:20px; " value="27865757000102"><!-- Coloquei o value já preenchido apenas para facilitar os testes durante o desenvolvimento -->
    					<input id="pesquisar" type="button" name="pesqui" value="Pesquisar"><!-- Aqui incluímos o id="pesquisar" para poder aplicar o evento pelo ID e mudamos o type para "button"--><br/><br/>
    			
    			<label for="nome">Nome fantasia</label><br/>
    				<input id="nome" type="text" name="nome" size="35" maxlength="50"><br/><br/>
    		</fieldset>
        
    		<br/><input id=save type="submit" name="salvar" value="Salvar">
    	</form>

3 answers

1

Since this element has ID you can simply use

document.getElementById('nome').disabled = true;
// ou para ficar só de leitura
document.getElementById('nome').readOnly = true;

Example:

$('#pesquisar').on('click', function(e) {
  e.preventDefault();
  document.getElementById('nome').readOnly = true;
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form id="formu">
  <p>Cadastro de Empresa</p>
  <fieldset id="first">

    <label for="cnpj">CNPJ</label><br/>
    <input id="cnpj" type="text" name="cnpj" size="35" maxlength="14" style="margin-right:20px; " value="27865757000102">
    <!-- Coloquei o value já preenchido apenas para facilitar os testes durante o desenvolvimento -->
    <input id="pesquisar" type="button" name="pesqui" value="Pesquisar">
    <!-- Aqui incluímos o id="pesquisar" para poder aplicar o evento pelo ID e mudamos o type para "button"--><br/><br/>

    <label for="nome">Nome fantasia</label><br/>
    <input id="nome" type="text" name="nome" size="35" maxlength="50"><br/><br/>
  </fieldset>

  <br/><input id=save type="submit" name="salvar" value="Salvar">
</form>

Note: Fields with disabled and fields with display: none; (previous to IE9) are not sent with the submit form.

  • remembering that disabled does not pass the value and readOnly raisin

  • @Leocaracciolo good observation. I added this and display: none; in case someone reads this without knowing these "side effects".

  • @Sergio Cara, I tested an input here with display:none and sent normal on Submit: <input type="text" name="teste" value="blabla" style="display:none;" />. The "Blabla" was usually far from the field being hidden.

  • @Dvdsamm in the old days was a known problem. I have been researching now and in fact from IE9 this no longer happens. (https://stackoverflow.com/a/8318442/2256325)

0

To block the change in that input, use jQuery to assign readonly (read-only) to the countryside.

Code:

$('#nome').prop('readonly', true);

0

For disable="disable" in input Ex:

<input type="text" id="" name="" value="exemplo" disabled="disabled"/>

Browser other questions tagged

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