Enable Disable Multiple Specific Fields

Asked

Viewed 569 times

0

Hello everyone As I have no experience with javascript I would like a help how to disable some specific fields.

In this Script below and is exactly what I need only when putting it in the code It disables fields that don’t need to be disabled. I would like to know how this code can be changed to only for some fields containing example.. an id=x

 <script type='text/javascript'>//<![CDATA[ 
 $(window).load(function(){
 $(document).ready(function() {
 $("#enable").click(function (){
            // habilita o campo 
    $("input").prop("disabled", false);
    $("select").prop("disabled", false);

 });

 $("#disable").click(function (){
            // desabilita o campo 
    $("input").prop("disabled", true);
    $("select").prop("disabled", true);
});
});
});//]]>  
</script>
<button href="#" id="enable" >Habilita Campos</button>
<button href="#" id="disable" >Desabilita Campos</button>

<input type="text" id="campo" name="campo1" disabled/> <br/>
<input type="text" id="campo" name="campo2" disabled/> <br/>
<input type="text"            name="campo3" disabled/> <!--exemplo Não fazer nada -->   
  • You cannot have different elements with the same ID. It’s always the same input you want to disable?

  • Let’s say I have 10 inputs, but I just want to enable/disable 4

  • What I want to know is how you choose them, so I can answer.

  • Yes I have to choose more I don’t know where you can make that choice, or if it could be like this, everyone who has id="1", id="2", id="3"

1 answer

1


To disable an input you have to use .prop("disabled", true); in a collection of elements.

To create a collection (which can have 1 or more elements) use the $('regras de CSS').

To choose all inputs you can do as you already have: $('input').
To choose by name: $('input[name="campo2"]').
To choose from multiple: $('input[name="campo2"], input[id="campo1"]').

Example: http://jsfiddle.net/vw4qns0x/

What’s missing from your question is how do you know which input you want to disable, but I hope the example will help.

Browser other questions tagged

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