Jquery enable submit button when changing input value

This is my part of my html

<input type="text" name="age" /> <input type="text" name="poscode" /> <input type="submit" name="submit" value="Next >>" disabled="disabled"/> 

This is my script

 <script type="text/javascript"> $(function(){ var enable = false; if($("input[name='age']").val != "") enable = true; if($("input[name='poscode']").val != "") enable = true; if(enable == true) $("input[name='submit']").attr('disabled', ''); }); </script> 

It doesn’t work, don’t know what am I doing wrong?

After the user has filled in two ages of input and poscode, the submit button should become active (disabled at startup)

+5
source share
6 answers

You can do something like this:

 $('input[name="age"], input[name="poscode"]').change(function(){ if ($(this).val()) { $("input[name='submit']").removeAttr('disabled'); } }); 
+11
source

Give this picture, here is the violin that I created so you can play with it.

http://jsfiddle.net/9sxwN/

+3
source

To enable / disable keystroke, follow these steps:

 $('input[name="age"], input[name="poscode"]').keyup(function(){ if ($(this).val()) { $("input[name='submit']").removeAttr('disabled'); }else{ $("input[name='submit']").attr('disabled','disabled'); } }); 
+2
source

Did you attach this to the input fields in any way? As your code is written, I believe that the function is called once, after the document is loaded, and then simply discarded. Give your function a name and attach it to onblur or onchange .

0
source

Try this sample code, please

 <div class="form-row align-items-center"> <div class="col-auto"> <input type="text" name="inputComment" class="form-control" id="inputComment" placeholder="Your comment goes here"> </div> <div class="col-auto"> <button disabled id="btnSaveComment" type="button" class="btn btn-success">Save</button> </div> </div> <script> document.getElementById("btnSaveComment").disabled = true; $(document).ready(function () { $("#inputComment").on("change paste keyup", function () { var text = $(this).val(); if (text.length > 0) { document.getElementById("btnSaveComment").disabled = false; } else { document.getElementById("btnSaveComment").disabled = true; } }); }); </script> 
0
source

As mentioned in sAc, you must completely remove the "disabled" attribute.

-1
source

Source: https://habr.com/ru/post/1316543/


All Articles