0

I want to disable an input on submit but it didn't work. I have a select and I want that if it has a certain value it won't be passed in parameter (here the value is "autres") and if this value is selected an input appear and user can type what they want. Here is what I have :

<select name="motif" id="motif" onChange="choix()">
                <option value="">-</option>
                <option value="formation">Formation</option>
                <option value="autres">Autres</option>
</select>
<div id="commentaire"></div>
<input type="submit" value="Envoyer ma demande" id="submit" onClick="disableMotif()"/>

Javascript

function choix()
{
    var i = document.getElementById("motif").value;
    var comment = document.getElementById("comment");
    if (i=='autres' && !comment)
      {
        $( "#commentaire" ).append( "<div id=\"comment\"> Indiquez le motif : <input type=\"text\" style=\"margin-top:5px;\" name=\"motif\" value=\"\"/></div>" );
      }
      else{
        comment.parentNode.removeChild(comment);
      }
  }
function disableMotif()
{
    var i = document.getElementById("motif").value;
    var comment = document.getElementById("comment");
    if (i=='autres' && !comment)
      {
        $("#motif").prop('disabled', true);
      }
}

The function for disable the input on submit doesn't work. I have in the parameters 2 times "motif". How can I do this ?

Simon M.
  • 1,732
  • 2
  • 13
  • 29

2 Answers2

1

Did you see this one ? Disable/enable an input with jQuery?

It can depends on your jQuery version. Try with the attr method instead of prop :

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

or directly :

$("#motif").disabled = true;
Community
  • 1
  • 1
Alexandre
  • 584
  • 3
  • 20
0

Well, just a wrong if condition. I needed to check only : if (i=='autres') and not if comment wasn't displayed

Simon M.
  • 1,732
  • 2
  • 13
  • 29