循环问题 [英] problem with loop

查看:73
本文介绍了循环问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好


我有以下功能,在页面加载时调用。

function checkFieldContent(form){

var field;

for(i = 0; i< form.elements.length; i ++){

field = form.elements [i];

if(field.type ==''text''){

alert(field.name);

// checkSearchInput(field); < br $>
}

提醒(''''');

}

alert(''end'') ;

}


像这样它完美但只要我自己的函数被调用 - >>

checkSearchInput(字段) );它只会循环一次,

checkSearchInput调用各种子函数,而且此刻不会返回任何东西。当我需要

在函数调用中返回true或false时,我不是真的很喜欢js并且很困惑。我尝试了返回

值(真或假),但它没有改变任何东西。当然功能

checkSearchInput本身可以正常工作...


非常感谢您的帮助!


sg2923

Hi there

I have following function which is called on load of page.
function checkFieldContent(form) {
var field;
for(i = 0; i < form.elements.length; i++) {
field = form.elements[i];
if (field.type == ''text'') {
alert(field.name);
// checkSearchInput(field);
}
alert(''after'');
}
alert(''end'');
}

Like this it works perfect but as soon as my own function is called ->
checkSearchInput(field); it''s only going one time through the loop,
checkSearchInput calls various subfunctions and at the moment does not
return anything. I''m not really into js and confused about when I need
to return true or false on a functions call. I tried it with a return
value (true or false) but it didn''t change anything. Of course function
checkSearchInput for itself works without errors...

Thanks a lot for your appreciated help!

sg2923

推荐答案

我们*** *@gmx.net 写道:

< snip>
us****@gmx.net wrote:
<snip>

function checkFieldContent(form){

var field;

for(i = 0; i< form.elements.length; i ++){

field = form.elements [i];

if(field.type ==''text''){

alert(field.name);

// checkSearchInput(field);

}

alert(''after '');

}

提醒(''结束'');

}


像这样它完美但只要我自己的函数被调用 - >>

checkSearchInput(field);它只有一次循环,

checkSearchInput调用各种子函数,目前确实没有返回任何东西。
function checkFieldContent(form) {
var field;
for(i = 0; i < form.elements.length; i++) {
field = form.elements[i];
if (field.type == ''text'') {
alert(field.name);
// checkSearchInput(field);
}
alert(''after'');
}
alert(''end'');
}

Like this it works perfect but as soon as my own function is called ->
checkSearchInput(field); it''s only going one time through the loop,
checkSearchInput calls various subfunctions and at the moment does
not return anything.



所以你可以发布一个让每个人都可以使用的代码

猜测实际导致代码的代码是什么问题?


无论如何;你没有声明你的循环计数器 - 我 - 作为一个

函数局部变量(所以它实际上是全局的)。如果在循环中调用的任何
函数使用类似的 - i - 变量,那么

也没有被声明为函数局部变量,并且任何设置它的

超出元素集合长度的值,循环将是

在下一次终止时全局变量 - i - 变量

是检查。

So you though you would post the code that works an leave everyone to
guess as to what is in the code that actually causes the problem?

In any event; you have not declared your loop counter - i - as a
function local variable (so it is effectively global). If any of the
functions called from within the loop use a similar - i - variable that
is also not declared as a function local variable, and any set its
value beyond the length of the elements collection, the loop will be
terminated on the next occasion the value of the global - i - variable
is checked.


我不是真的进入js ...
I''m not really into js ...



< snip>


使用局部变量应该使用的全局变量是一般的编程中的错误

,因此与JS无关。


Richard。

<snip>

Using global variable where local variables should be used is a fault
in programming in general, so not related to JS as such.

Richard.


我们**** @ gmx.net 写道:
us****@gmx.net writes:

我有以下函数在页面加载时调用。

函数checkFieldContent(表单) ){

var field;

for(i = 0; i< form.elements.length; i ++) {
I have following function which is called on load of page.
function checkFieldContent(form) {
var field;
for(i = 0; i < form.elements.length; i++) {



这里i未声明为局部变量。这意味着

它被创建为全局变量。你应该总是声明

您的变量只有所需的范围,即

将其更改为:


for (var i = 0; i< form.elements.length; i ++){

Here "i" is not declared to be a local variable. That means that
it is created as a global variable. You should always declare
your variables to have only as much scope as needed, i.e.,
change this to:

for(var i = 0; i < form.elements.length; i++) {


// checkSearchInput(field);
// checkSearchInput(field);



很可能你在checkSearchInput中有一个类似的循环,

表示全局i的值。通过调用

将变量更改为大于form.elements.length的值。修复那个方法。


(如果调用函数会使你的脚本中断,那么当你提问时包含该函数可能是一个很好的想法求助:)


/ L

-

Lasse Reichstein Nielsen - lr*@hotpop.com

DHTML死亡颜色:< URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>

''没有判断的信仰只会降低神灵的精神。''

Most likely you have a similar loop in the "checkSearchInput", which
means that the value of the global "i" variable is changed by the call
to something larger than "form.elements.length". Fix that method too.

(If a call to a function makes your script break, it might be a good
idea to include that function when you ask for help :)

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
''Faith without judgement merely degrades the spirit divine.''


于2006年8月18日写在comp.lang.javascript
wrote on 18 aug 2006 in comp.lang.javascript:

我有以下函数,在页面加载时调用。

函数checkFieldContent(form){

var field;

for(i = 0; i< form.elements.length; i ++){

field = form.elements [i];

if(field.type ==''text''){

alert(field.name);

// checkSearch输入(字段);

}

提醒(''''');

}

alert( ''结束'');
}

像这样它完美但只要我调用自己的函数 - >

checkSearchInput (领域);它只会循环一次,

checkSearchInput调用各种子函数,而且此刻不会返回任何东西。当我需要

在函数调用中返回true或false时,我不是真的很喜欢js并且很困惑。我尝试了返回

值(真或假),但它没有改变任何东西。当然功能

checkSearchInput本身可以正常工作......
I have following function which is called on load of page.
function checkFieldContent(form) {
var field;
for(i = 0; i < form.elements.length; i++) {
field = form.elements[i];
if (field.type == ''text'') {
alert(field.name);
// checkSearchInput(field);
}
alert(''after'');
}
alert(''end'');
}

Like this it works perfect but as soon as my own function is called ->
checkSearchInput(field); it''s only going one time through the loop,
checkSearchInput calls various subfunctions and at the moment does not
return anything. I''m not really into js and confused about when I need
to return true or false on a functions call. I tried it with a return
value (true or false) but it didn''t change anything. Of course function
checkSearchInput for itself works without errors...



你永远不应该调用变量form。

将表格视为保留字。


在这段代码中你会伤到自己,

,因为你用外部值填充表格

一个名为field

的变量,可能被定义为一个字段

而不是一个表单。

尝试:


==============================

< script type =''text / javascript''>


function checkFieldNames(myForm){

var myForm = document.getElementById(''thisForm '')

var myField;

for(var i = 0; i< myForm.elements.length; i ++){

myField = myForm.elements [i];

if(myField.type ==''text'')

alert(myField.name);

}

}


< / script>


< body onload =''checkFieldNames( & thisForm)''>


< form id =''thisForm''>

< input name =''t1' '>

<输入名称=''t2''>

<输入类型=''提交''name ='''跳过''>

<输入名称=''t3''>

< / form>

========= ========================


for(i = 0; ...

应为(var i = 0; ...

但这只会导致问题

如果你有一个全局变量i,

即使这样,上面的函数也不会受到影响。


< input type =''text''中的类型声明是默认的。


-

Evertjan。

荷兰。

(请将x''es更改为我的电子邮件地址中的点数)

You should never call a variable "form".
Treat form as a reserved word.

In this code you hurt yourself,
as you fill the form with a external value
of a variable called field
that probably is defined as a field
and not as a form.
Try:

==============================
<script type=''text/javascript''>

function checkFieldNames(myForm) {
var myForm = document.getElementById(''thisForm'')
var myField;
for(var i = 0; i < myForm.elements.length; i++ ) {
myField = myForm.elements[i];
if (myField.type == ''text'')
alert(myField.name);
}
}

</script>

<body onload=''checkFieldNames("thisForm")''>

<form id=''thisForm''>
<input name=''t1''>
<input name=''t2''>
<input type=''submit'' name=''skipped''>
<input name=''t3''>
</form>
=================================

The for(i = 0;...
should be for(var i = 0;...
but that will only cause problems
if you have a global variable i,
and even then the above function will not be affected.

The type declaration in <input type=''text'' is default.

--
Evertjan.
The Netherlands.
(Please change the x''es to dots in my emailaddress)


这篇关于循环问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆