0

I'm trying to get this form to execute this function on submit but for some reason, it pops the alert up, but then refreshes the entire page. I've tried other online solutions like: Prevent form redirect OR refresh on submit?

But that did not work.

function addFood() {
    alert("123");
    return false;
}

<form onsubmit="return addFood()" data-ajax="false">
    <input type="text" name="" id="name" value="" placeholder="name" data-clear-btn="true"/>
    <input type="text" name="" id="brand" value="" placeholder="brand" data-clear-btn="true"/>
    <input type="text" name="" id="extra" value="" placeholder="extra" data-clear-btn="true"/>
    <input type="number" name="" id="amount" value="" placeholder="amount" data-clear-btn="true"/>
    <input type="number" name="" id="calories" value="" placeholder="calories" data-clear-btn="true"/>
    <input type="number" name="" id="fat" value="" placeholder="fat" data-clear-btn="true"/>
    <input type="number" name="" id="carbohydrate" value="" placeholder="carbohydrate" data-clear-btn="true"/>
    <input type="number" name="" id="fibre" value="" placeholder="fibre" data-clear-btn="true"/>
    <input type="number" name="" id="sugar" value="" placeholder="sugar" data-clear-btn="true"/>
    <input type="number" name="" id="protein" value="" placeholder="protein" data-clear-btn="true"/>
    <input type="submit" value="submit"/>
<form>
Community
  • 1
  • 1
eveo
  • 2,797
  • 15
  • 61
  • 95
  • Can you post your entire code? It must be some other jQuery Mobile related setting messing things up (I have a suspicion it might by AJAX navigation). Anyhow I just tried this on http://jquerymobile.com/demos/1.2.1/docs/pages/page-template.html and it seems to work fine. – Waleed Amjad Nov 11 '13 at 23:20

1 Answers1

0

Remove onsubmit and set your <form>'s action attribute to # (I would also add an id)

<form id="myForm" action="#" data-ajax="false">

If you need something to happen when your <form> is submitted, use:

$("#myForm").submit(function(){
    // do stuff that will fire off when my form is submitted
});
FastTrack
  • 8,810
  • 14
  • 57
  • 78
  • That does not do anything. The page still refreshes, and whatever I'm calling inside my form submit jquery, does not execute. – eveo Nov 11 '13 at 22:44