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();
});
Delmon Young
  • 2,013
  • 5
  • 39
  • 51
  • Sorry I didn't get what you want.. – Arun P Johny Dec 11 '13 at 02: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. – Delmon Young Dec 11 '13 at 02: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); } – Neha Dec 11 '13 at 03:05

3 Answers3

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();
});
ProllyGeek
  • 15,517
  • 9
  • 53
  • 72
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?

Community
  • 1
  • 1
Stephen Bunch
  • 327
  • 4
  • 15
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

Neha
  • 1,548
  • 2
  • 10
  • 13