I am attempting to run a function
only if a submit button has been pressed (which sets the integer value of the variable submitted to 1), however I am receiving this error in the console and the function isn't running after the variable being set to 1:
Uncaught ReferenceError: submitted is not defined
$("form#form1").submit(function(event) {
event.preventDefault();
var submitted = 1; // set the integer value of the variable submitted to 1
});
if (submitted == 1) { // if the integer value of the varialbe is 1
// run a function
}
<script src=".1.1/jquery.min.js"></script>
<form id="form1">
<input type="submit">
</form>
I am attempting to run a function
only if a submit button has been pressed (which sets the integer value of the variable submitted to 1), however I am receiving this error in the console and the function isn't running after the variable being set to 1:
Uncaught ReferenceError: submitted is not defined
$("form#form1").submit(function(event) {
event.preventDefault();
var submitted = 1; // set the integer value of the variable submitted to 1
});
if (submitted == 1) { // if the integer value of the varialbe is 1
// run a function
}
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form id="form1">
<input type="submit">
</form>
Share
Improve this question
edited Feb 4, 2017 at 14:09
The Codesee
asked Feb 4, 2017 at 13:56
The CodeseeThe Codesee
3,7937 gold badges44 silver badges80 bronze badges
1
- 1 When exactly do you want to run the function? If you want to run it when the submit button is pressed, just call it in the submit event. – JJJ Commented Feb 4, 2017 at 14:10
3 Answers
Reset to default 2You should just put the call to the function right in the callback of the submit action:
$("form#form1").submit(function(event) {
event.preventDefault();
// run a function here
});
This way the function will be called right after the form is submitted.
There are two problems:
submitted
is local to yoursubmit
handler because you declared it within the handler function, so it's not accessible outside it.If we just fix #1, your
if
statement will run whensubmitted
still has whatever initial value we give it in #1, it won't be the value from thesubmit
handler as that handler hasn't been run yet. Instead, that code needs to be run in response to some event, so that if the event it's running in response to is after a submission.
Just for illustration, here's an example running the if
from a click on a button:
var submitted = 0;
$("form#form1").submit(function(event) {
event.preventDefault();
submitted = 1; // set the integer value of the variable submitted to 1
});
$("#btn").on("click", function() {
if (submitted == 1) {
$("<p>Submitted</p>").appendTo(document.body);
} else {
$("<p>Not yet submitted</p>").appendTo(document.body);
}
});
Live on jsFiddle (Stack Snippets don't allow form submissions, not even cancelled ones.)
Your variable is locally scoped to the submit function. Set it to 0 from the outset and then change it when the form is submitted. You have to globally scope the variable.
EDIT: The ments are correct. I didn't take a minute to think about this one. I'm not sure, but I think jQuery's submit handler only runs on submit and has no success/error handler function; so I would either use a different function to handle it, or just stick your function inside the submit callback and not worry about the submitted
variable at all.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744898446a4599835.html
评论列表(0条)