首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >(JS_console)当点击按钮时,为什么这个循环会中断?

(JS_console)当点击按钮时,为什么这个循环会中断?
EN

Stack Overflow用户
提问于 2014-02-19 14:34:48
回答 3查看 66关注 0票数 0

我想要在控制台中执行这段代码,我想要做的是更改一些字段的值并继续提交。

代码语言:javascript
运行
复制
var animal = new Array(); //Declare Array 

animal[0] = 'cat';
animal[1] = 'dog';
animal[2] = 'cow';
animal[3] = 'snake';
animal[4] = 'goat';
animal[5] = 'tiger';
animal[6] = 'lion';
animal[7] = 'horse';


var createFunc = function(i){
    return function(){
        var token_field = document.getElementById('animal_name'); 
        token_field.value = animal[i]; //Change the value of the field with each array value

        var path_field = document.getElementById('To_do');
        path_field.value='feed'; // Change the other field with 'feed'

        var submit_all = document.getElementById('graph_submit');
        submit_all.click(); //submit the form
    };
};
for (var i = 0; token.length > i; i++) {
    setTimeout(createFunc(i), 2000);
}

但当单击按钮时,页面会重新加载并中断循环……因此,只有第一个值被提交,其他值保留。

第一次提交(超过2秒)后,提交页面的代码停止执行。由于POST查询,页面将被重新加载。但是我想要提交所有的值,但是页面被重新加载。

我能做些什么呢?

EN

Stack Overflow用户

发布于 2014-02-19 14:43:23

单击“graph_submit”作用域时,有可能发生更改,循环中断。

您可以在此处使用防止默认功能。

票数 0
EN
查看全部 3 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21872870

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档