仅当按下提交按钮(将提交的变量的整数值设置为 1)时,我才尝试运行函数
,但是我在控制台中收到此错误,并且变量设置为 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.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form id="form1">
<input type="submit">
</form>
最佳答案
您应该将对函数的调用放在提交操作的回调中:
$("form#form1").submit(function(event) {
event.preventDefault();
// run a function here
});
这样,该函数将在提交表单后立即调用。
关于javascript - Uncaught ReferenceError : submitted is not defined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42041203/