简体   繁体   English

当我在add函数中添加Submit函数时,为什么该程序无法工作?

[英]Why does the this program not work when I add the submit function inside the add function?

It works until I add the click function inside the add function. 直到我将add函数添加到click函数中之前,此方法一直有效。

How to make it work such that when add function is called from within the check function the flag is incremented only when the user clicks the submit button and then the control is passed back to check function so the correct final value should be 3. 如何使其工作,以使得当从检查功能中调用添加功能时,仅当用户单击提交按钮时该标志才递增,然后将控件传递回检查功能,因此正确的最终值应为3。

 var flag = 1; if(flag == 1) { $('p').html("xxx"); check(); } function check() { $('#submit').click(function() { if($("#text").val()=="") { alert("Enter some text"); } else { add(); flag++; alert(flag); } }); } function add() { $('#submit').click(function() { flag++; alert("inside add " + flag); }); } 
 <script src="https://code.jquery.com/jquery-3.3.1.min.js" integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8=" crossorigin="anonymous"></script> <div id="myform"> <p id="title"></p> <input type="text" id="text"> <input type="submit" id="submit"> </div> 

You need to add $( document ).ready(function() { ... }); 您需要添加$( document ).ready(function() { ... });

this seems to work as you aspected: 这似乎在您方面起作用:

  $( document ).ready(function() { var flag = 1; if(flag == 1) { $('p').text("xxx"); check(); } function check() { $('#submit').click(function() { if($("#text").val()=="") { alert("Enter some text"); } else { add(); flag++; alert(flag); } }); } function add() { $('#submit').click(function() { flag++; alert("inside add " + flag); } )}; }); 
 <script src="https://code.jquery.com/jquery-3.3.1.min.js" integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8=" crossorigin="anonymous"></script> <div id="myform"> <p id="title"></p> <input type="text" id="text"> <input type="submit" id="submit"> </div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当我将JavaScript块添加到函数中时,该块不起作用。 有人可以帮忙解释原因吗? - A block of JavaScript does not work when I add it to a function. can anybody help explain why? 为什么我的添加功能不起作用? - Why does my add function not work? 为什么添加新功能后我以前的功能不起作用? 我怎样才能解决这个问题? - Why does my previous function don't work when I add a new function? How can I fix this? 当我按空白表格提交时,此功能不起作用 - The function does not work when i press submit with empty form 当我更新此函数内的状态时,油门函数不起作用 - throttle function does not work when i update state inside this function 添加新列表项时,创建的切换功能不起作用 - The toggle function I created does not work when I add new list items 为什么在 object 中调用 function 时“this.myFunction”不起作用? - Why does “this.myFunction” not work when calling a function inside an object? 为什么此代码仅在变量位于 function 内时才有效? - Why does this code only work when the variables are inside the function? 为什么用 `$(&quot;#submitButton&quot;).submit(function(){})` 绑定提交事件不起作用? - Why does binding a submit event with `$("#submitButton").submit(function(){})` not work? 我无法在函数内添加函数 - I cannot add a function inside a function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM