Looking for a hand on my jQuery and JS script

What I have in my fiddle here is http://jsfiddle.net/mPuj9/4/

What I tried to do was disable the button until all textbox es are full and checkbox is checked. My current code is not very good, it allows you to send in some cases, and I don’t know how to work with checkbox .

CODE

 $(document).ready(function() { $('.textfield').blur(function() { if ($.trim(this.value) == "") { $('#btnUpdate').attr("disabled", true); } else { $('#btnUpdate').removeAttr("disabled"); } }); }); $(document).ready(function() { $('#termid').change(function() { var val = $(this).val(); $('#reservationdetails').empty().addClass('loading').load('../kratisis/forms/' + val + '.php', function() { $('#reservationdetails').removeClass('loading') }); }); }); 
+4
source share
3 answers

Here you go

http://jsfiddle.net/mPuj9/8/

 $(document).ready(function () { $('form').delegate('input:text, input:checkbox', 'blur keyup change', function () { if(($('form input:text').filter(function(){ return $.trim(this.value) == ''; }).length > 0) || ($('form input:checked').length == 0)){ $('#btnUpdate').attr("disabled", true); } else { $('#btnUpdate').removeAttr("disabled"); } }); }); 
+2
source

Here you can make another way, which also works if someone presses the enter button in a text field or if the browser automatically completed the input:

 $(function() { $("form button").attr("disabled", true); //disable buttons at first var inputs = 0; //keep count of the inputs $("form input, form select, form textarea").each(function() { inputs++; //increment counter $(this).keypress(function() { if ($.trim($(this).val()) != "") { //if the value isnt empty inputs--; //decrement counter if (inputs == 0) { //if theyre all filled in $("form button").attr("disabled", false); } } }).trigger("keypress"); //in case it was autofilled by the browser }); $("form").submit(function(e) { //prevent the form being submitted by pressing enter if (inputs != 0) { //if they arent all filled in alert("Some fields aren't filled in. Please fix them and try again."); //tell the user e.preventDefault(); //cancel sending the form return false; } }); }); 
+1
source

I think the problem is that you are not using .each () for text fields. You check the one that was recently modified to find out if it is a value of "", but you do not check each of them.

0
source

All Articles