SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Member
    Join Date
    Jun 2008
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    If statement based on function return

    Hello all,

    First of all I really hope someone can help, this is probably (I'm hoping) really simple but I just can't get it! I appreciate all the help thank you!

    I am working on a form submit button, when it is clicked the first time it is disabled, so multiple submissions cannot occur before the confirmation page loads. There is also a checkForm function that validates the form ensuring that required fields are required.

    Anyway I need to add an If statement so that if the function returns false (as in the form does not submit) then the button is re-enabled.

    Hope this makes some sense, thanks in advance.

    Jess

    Code:
    if(checkForm == false) {
    	$('#submit').removeAttr("disabled");
    }

  2. #2
    SitePoint Enthusiast
    Join Date
    Aug 2010
    Posts
    29
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You should post the html code for button and the javascript function.

    Tried this?
    Code:
       if ( some condition) {
            $('#submit').attr('disabled', true);
        } 
        else {
            $('#submit').removeAttr('disabled');
        }

  3. #3
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,813
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    If checkform is the function whose return value you want to check then checking if it is false would use:

    if(!checkForm()) {
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  4. #4
    SitePoint Member
    Join Date
    Jun 2008
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for your responses folks, I'm still having some problems though. Here is the code I have:

    Code:
    <script type="text/javascript">
      $(function() {
    	$('#submit').click(function() {
    	  $(this).attr('disabled','disabled');
       	  $('#loading').remove()
    				
    	  $('#submission').append('<img src="loading.gif" alt="Currently Loading" id="loading" />');				
    	  if(!checkForm()) {
     	     $('#submit').removeAttr("disabled");
    	  }
           });
      });
    </script>
    
    
    <form name="appForm" id="appForm" method="post" action="****" onsubmit="return checkForm()" enctype="multipart/form-data">
    
    <input type="text" required="text" name="test" id="test"/>
    
    <div id="submission"><input type="submit" name="submit" id="submit" value="Submit Application"/> <input type="reset" id="reset" name="reset" value="Clear Form"/>
    <br/>
    </div>
    <span style="color:red;">*Please only submit this form once.  You will be taken to a confirmation page once the form has been submitted.*</span>
    </form>

  5. #5
    SitePoint Enthusiast
    Join Date
    Aug 2010
    Posts
    74
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    try this :

    HTML Code:
    <script type="text/javascript">
      $(function() 
      {
    	$('#appForm').submit(function() {
    		if(!checkForm())return false;
    		
    	  $('#submit').attr('disabled','disabled');
       	  $('#loading').remove();
    	  $('#submission').append('<img src="loading.gif" alt="Currently Loading" id="loading" />');
    	  return true;				
           });
      });
      
      function checkForm()
      {
    	return $('#test').attr('value') != '';
      }
    </script>
    
    <form name="appForm" id="appForm" method="post" action="javascript:alert('submited');"  enctype="multipart/form-data">
    
    <input type="text" required="text" name="test" id="test"/>
    
    <div id="submission"><input type="submit" name="submit" id="submit" value="Submit Application"/> <input type="reset" id="reset" name="reset" value="Clear Form"/>
    <br/>
    </div>
    <span style="color:red;">*Please only submit this form once.  You will be taken to a confirmation page once the form has been submitted.*</span>
    </form>


Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •