javascript - Uncaught ReferenceError: submitted is not defined - Stack Overflow

I am attempting to run a function only if a submit button has been pressed (which sets the integer valu

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
Add a ment  | 

3 Answers 3

Reset to default 2

You 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:

  1. submitted is local to your submit handler because you declared it within the handler function, so it's not accessible outside it.

  2. If we just fix #1, your if statement will run when submitted still has whatever initial value we give it in #1, it won't be the value from the submit 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条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信