0

Im trying to disable an input after a condition and i dont understand why this not working. I tried to use $("#exemplaire_50001_0").attr('disabled', true); and $("#exemplaire_50001_0").prop('disabled', true); but both are changing nothing. Im using Jquery 1.5.1 so i think .attr must be used.

When i add $("#exemplaire_50001_0").css("background-color", "red"); to my selector so the background is changing to red.

My question is, why the .css working but the .prop or .attr is not ? Where is the problem come from ? Is there an other solution to disable an input with jquery ?

Code :

 var test = $('#num_cppap').val();
 var test2 = test.substr(0,3);
 console.log(test2);
 if(test2 == "AIP"){
     $("#exemplaire_50001_0").css("background-color", "red");
     $("#exemplaire_50001_0").attr('disabled', true);
 }

html ( this how i build the html ( using ZendFramework ) ) :

enter image description here

Nav render :

enter image description here

3 Answers3

0

As mentioned here :

jQuery 1.5 and below

The .prop() function doesn't exist, but .attr() does similar:

Set the disabled attribute.

$("input").attr('disabled','disabled'); 

To enable again, the proper method is to use .removeAttr()

$("input").removeAttr('disabled');

var text = $("#text");
var btn = $("#btn");

btn[0].onclick = function() {
 text.attr('disabled', 'disabled');
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.5.1/jquery.min.js"></script>

<input id="text" value="toto" />

<button id="btn">
  click me
</button>
robinvrd
  • 1,390
  • 8
  • 25
0

Can you look at this please, i maked an exemple :

    var id = $("#exemplaire_50001_0");
    var test = $('#num_cppap').val();
    //var test2 = test.substr(0, 3);
    //console.log(test2);
    //if(test2 == "AIP"){
    $("#exemplaire_50001_0").css("background-color", "red");
    id.attr('disabled', 'disabled');
    //}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.5.1/jquery.min.js"></script>
<div id="tableNiveau" class="declare">
  <table id="tableNivPrep" class="tabData" border="0" style="display:block">
    <thead>
      <tr class="entete">
        <th colspan="2" rowspan="2" class="entete">Préparation</th>
        <th colspan="2" class="entete">Déclaré</th>
        <th colspan="2" rowspan="2" class="entete">Option</th>
        <th colspan="2" rowspan="2" class="entete" style="width:20%">Offre grand compte</th>
      </tr>
      <tr class="entete">
        <th class="entete">Exemplaires</th>
        <th class="entete">Paquets</th>
      </tr>
    </thead>
    <tbody>
      <tr id="50001" class="odd">
        <td>Edition LDQL</td>
        <td></td>
        <td><input type="hidden" value="" name="idPrepa_50001_0"><input type="text" value="" name="exemplaire_50001_0" id="exemplaire_50001_0" class="saisie nivPrep Edition" style="text-align: right; background-color: red;" onkeyup="calculTotauxDeclare()"
            onkeypress="return chiffres(event);"></td>
        <td></td>
        <td></td>
        <td><select name="option_preparation_50001_0" id="option_preparation_50001_0"><option value="">Choix de l'option</option><option value="81000800">LOCAL                                             </option><option value="81000801">Standard                                          </option><option value="81000802">DIRECT                                            </option><option value="81000803">DepotCTC                                          </option><option value="81000804">DepotCDIS                                         </option></select></td>
        <td
          style="text-align:center;"><input type="checkbox" name="zone_dense_50001" id="zone_dense_50001">
          </td>
      </tr>
-2

you have to correct your code like below:

from

$("#exemplaire_50001_0").attr('disabled', true);

to

$("#exemplaire_50001_0").attr('disabled', 'disabled');

above code is for disable and if you want to enable it you have to remove disable attribute like below code :

$("input").removeAttr('disabled');
Atabai Fekri
  • 155
  • 2
  • 2
  • 15
  • @robinvrd no it isn't. Any non-empty value for the attribute will disable the ``. – Pointy Jun 03 '19 at 13:43
  • @robinvrd nobody's "trash talking" here. It's simply true that any non-empty string works for the "disabled" attribute value and it always has. – Pointy Jun 03 '19 at 14:00