1

Sorry bad title. I've got a simple form with an input field and I know that every time I submit the form the number of arguments that gets passed is one. When the argument of one gets passed reload gets evaluated to true and if reload is true the page get's refreshed. The problem is that what if I don't want to refresh the page. What if I want reload to get evaluated to false in certain instances. Is there some parameter I can pass into the userInput function that would evaluate certain scenarios to false? Any ideas? I learn best by code samples, please include where applicable. I also have a fiddle going here:

http://jsfiddle.net/JuG48/2/

<form id="myForm">
  <input id="address" name="address" value="address">
  <input type="submit" name="submit" value="submit">
</form>

function userInput(userLocation, input){
  $('#myForm').on('submit', function(e){
  reload = arguments.length > 1 ? arguments[1] : true;
  if (reload) location.reload();
  console.log(reload);
});
}

$(document).ready(function () {    
    userInput();
});
3
  • Sorry I didn't get what you want.. Commented Dec 11, 2013 at 2:34
  • I'm trying to figure out if there's a way I can pass in a parameter to the function that will return false in certain instances. So could I do something like userInput(false) That's just a guess though. Commented Dec 11, 2013 at 2:41
  • I think u have to set some cond where it will false inside the function like : function somemethod(p1, p2) { reload =(arguments.length<1 ? false: (arguments.length > 1 ? arguments[1] : true); } Commented Dec 11, 2013 at 3:05

3 Answers 3

1

I think in your case the quickest solution is garlic.js

http://garlicjs.org/

<script type="text/javascript">
  $( 'form' ).garlic();
</script>

a native solution will be

$('#myForm').submit(function () {
 reload = arguments.length > 1 ? arguments[1] : true;
 if (reload) location.reload();
 console.log(reload);
 e.preventdefault();
});
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks @ProllyGeek! I was looking for more of a native solution which seemed to do the trick.
0

The page will automatically refresh or follow the post action url when a form is submitted. I'm confused about the line that conditionally reloads the page. Have you tried returning false in the submit handler to control the page reload? How do I cancel form submission in submit button onclick event?

Comments

0

Suppose you want no argument in userInput () method call to be reload = false so do this way ...

function userInput(userLocation, input){
$('#myForm').on('submit', function(e){

   reload = (userLocation==undefined && input==undefined)? false 
   :(arguments.length > 1 ? arguments[1] : true);       
   console.log(reload);
   if (reload) location.reload();
   else return false;      
  });
}

$(document).ready(function () {    
userInput();
 });

Fiddle demo

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.