简体   繁体   English

在jQuery中多次调用一个函数

[英]Calling a function more than once in jQuery

I have a function called "go()" which needs to be called thrice on page load. 我有一个名为“ go()”的函数,在页面加载时需要调用三次。 Right now I have called it on the ready event of a div with class "nav". 现在,我在具有“ nav”类的div的ready事件中调用了它。 Can someone suggest how can I call the go function thrice. 有人可以建议我如何三次调用go函数。 Adding go() calls one after other calls it only once. 添加go()将一个接一个地调用一次。

$('.nav').ready(function() {
     go();
    });

Even though you say this does not work, it should... 即使您说这行不通,也应该...

$('.nav').ready(function() {
     go();
     go();
     go();
    });

Update: If there is an error in your go() function, it might terminate execution before go() returns for the first time. 更新:如果go()函数中存在错误,则它可能会在go()首次返回之前终止执行。 Run in Firebug and see if you are getting any errors. 在Firebug中运行,看看是否遇到任何错误。

$('.nav').ready(function() {
     go();
     go();
     go();
    });

Should call it three times. 应该叫三遍。

As other people have stated, placing the calls one-after-the-other should have called it three times. 正如其他人所说的那样,一个接一个的呼叫应该已经呼叫了三次。 Perhaps there something that go() is doing that is not being done more than once due to how the function is called, etc. It might be helpful to take a closer look at go() - please post that code and/or explain why you need to call it 3 times. 也许由于函数的调用方式等原因,go()正在执行的操作不会多次执行。仔细研究go()可能会有所帮助-请发布该代码和/或解释原因您需要调用3次。

I think I understand what you're trying to do...in the best terms as I can possibly describe since I've not taken a single jQuery class, just learned bits and pieces on my own through places like this website. 我想我理解您正在尝试做的...用我可能能描述的最好的术语,因为我没有上过一个jQuery类,而是通过像本网站这样的地方自己学习了点点滴滴。 I was doing something similar. 我在做类似的事情。 I needed a datepicker to call twice, for two different fields that needed a calendar to pop up - for a check-in and check-out date in a form . 我需要一个datepicker来调用两次,以用于需要弹出日历的两个不同字段- form入住退房日期 In my head I did this, I made two separate scripts with two separate and different id values: 在我的脑海中,我制作了两个具有两个不同id值的独立脚本:

<script>
   $( function() {
     $( "#datepicker" ).datepicker();
   } );
</script>  


<script>
 $( function() {
   $( "#datepicker2" ).datepicker();
 });
</script> 

And my form in the body of my webpage looks like this: 我的网页bodyform如下所示:

For my Check-In Date: 我的入住日期:

<input name="DateIn" type="text" id="datepicker" style="width:90px"/>

For my Check-Out Date: 对于我的退房日期:

<input name="DateOut" type="text" id="datepicker2" style="width:90px">

So I guess you need to make 3 scripts each with #go1 , #go2 , #go3 in your head and corresponding calls in your html . 因此,我想您需要制作3个脚本,每个脚本的头部为#go1#go2#go3 ,并且在html相应的调用。 I hope this helps. 我希望这有帮助。

Like rikh sayd, you must be mistaken. 就像里奇所说的那样,你一定是误会了。

Please test that you can see three hello-s when you replace your go() function with something like this: 请测试当用以下内容替换go()函数时,是否可以看到三个hello -s:

function go() {
  alert("hello");
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM