字符串的值??? [英] Value of string???

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

问题描述

我有一个包含40个文本框的表单(默认值为每个= 0)。我想

评估每个框以查看该值是否仍然为零,如果是,我将b $ b想要将其添加到零计数。盒子名称是L411至L419,

L421至L429,L431至L439和L431至L439。和L441至L449。


例如:


L41 - 002000500

L42 - 000030000

L43 - 000000000

L44 - 000000000


我通过几个嵌套for循环构造控件名称

并且一切都很好(构造的string =" MyForm.L411.value")。

当我将其插入到If语句中以获取控件的值时,

一切都会死亡。这是我的If:


for(var j = 1; j< 5; j ++)

{

rowString = L4;


//建立L41

rowString = rowString + j;


for(var x = 1; x <10; x ++)

{

//建立L411

rowString = rowString + x;


//构建MyForm.L411.value

rowString =" MyForm。" + rowString +" .value"


// alert显示MyForm.L411.value - 没有引号

// alert(rowString);


if(rowString =" 0")

{

zeroCount ++:


}

rowString =" L4" + j;

}

}


如何根据构建的字符串测试控件的值?


很多tia'的

ck

I have a form with 40 text boxes (default of each = 0). I want to
evaluate each box to see if the value is still zero, and if it is, I
want to add it to a "Zero Count". The box names are "L411 thru L419",
"L421 thru L429", "L431 thru L439" and "L441 thru L449".

Example:

L41 - 002000500
L42 - 000030000
L43 - 000000000
L44 - 000000000

I have construct the control name thru a couple of nested for loops
and all is well (constructed string = "MyForm.L411.value").

When I plug this into an If statement to get the value of the control,
everything dies. Here is my If:

for(var j = 1; j < 5; j++)
{
rowString = "L4";

// builds L41
rowString = rowString + j;

for(var x = 1; x < 10; x++)
{
// builds L411
rowString = rowString + x;

// Builds MyForm.L411.value
rowString = "MyForm." + rowString + ".value"

//alert shows "MyForm.L411.value" - without quotes
//alert(rowString);

if( rowString = "0")
{
zeroCount++:

}
rowString = "L4" + j;
}
}

How can I test the value of a control, based on a string built?

Many tia''s

ck

推荐答案

ck****@fusert.net 写道:
我有一个包含40个文本框的表单(默认值为每个= 0)。我想评估每个框以查看该值是否仍为零,如果是,我想将其添加到零计数。盒子名称是L411至L419,L421至L429,L431至L439,L431至L439。和L441至L449。

例如:

L41 - 002000500
L42 - 000030000
L43 - 000000000
L44 - 000000000

我已经通过几个嵌套的for循环来构造控件名称
并且一切都很好(构造的string =" MyForm.L411.value")。
当我将其插入到If语句中以获取控件的值时,
一切都会消失。这是我的If:

for(var j = 1; j< 5; j ++)
{
rowString =" L4" ;;
//构建L41
rowString = rowString + j;

for(var x = 1; x< 10; x ++)
//
//构建L411
rowString = rowString + x;

//构建MyForm.L411.value
rowString =" MyForm。" + rowString +" .value"


哎呀...你已经把''rowString''作为文字字符串

" MyForm.L411.value"这不是你想要做的。


你想要的是:


rowString = MyForm.rowString.value;


假设表单的元素名称等于''rowString''的值

,即L411。


但是这很危险,测试元素是否存在然后尝试获得它的价值,''如果它没有,那么你的脚本就会死掉:


if(MyForm.rowString){

rowString = Myform.rowString.value;

} else {

rowString = undefined;

}


可以缩写为:


rowString =(MyForm.rowString&& MyForm .rowString.value);


我建议将名称更改为rowValue,但那只是我。

以这种方式重用变量可能会使维护更难,但是再次b / b
,这取决于你。在名为''eleName''的变量中构建元素

名称可能会更好,然后将值赋给

''eleValue''所以事情是更清楚,你会得到:


eleValue =(MyForm.eleName&& MyForm.eleName.value);

//警报显示" MyForm.L411.value" - 没有引号
// alert(rowString);

if(rowString =" 0")


而不是测试是否值''rowString''相当于

字符串''0'',你给它分配字符串''0'的值。


作为提示总是写下这样的测试:


if(''0''= rowString)


这样你会得到一个脚本错误而不仅仅是错误

结果。正确的语法(假设您要测试相等性)

是:


if(''0''== rowString)

{
zeroCount ++:

}
rowString =" L4" + j;
}

如何根据构建的字符串测试控件的值?

很多tia''

ck
I have a form with 40 text boxes (default of each = 0). I want to
evaluate each box to see if the value is still zero, and if it is, I
want to add it to a "Zero Count". The box names are "L411 thru L419",
"L421 thru L429", "L431 thru L439" and "L441 thru L449".

Example:

L41 - 002000500
L42 - 000030000
L43 - 000000000
L44 - 000000000

I have construct the control name thru a couple of nested for loops
and all is well (constructed string = "MyForm.L411.value").

When I plug this into an If statement to get the value of the control,
everything dies. Here is my If:

for(var j = 1; j < 5; j++)
{
rowString = "L4";

// builds L41
rowString = rowString + j;

for(var x = 1; x < 10; x++)
{
// builds L411
rowString = rowString + x;

// Builds MyForm.L411.value
rowString = "MyForm." + rowString + ".value"
Ooops... you have made ''rowString'' the literal string
"MyForm.L411.value" which is not what you want to do.

What you want is:

rowString = MyForm.rowString.value;

presuming that the form has an element with a name equal to the value
of ''rowString'', i.e. L411.

But this is dangerous, test that the element exists before trying to
get its value, ''cos if it doesn''t, your script will die:

if ( MyForm.rowString ) {
rowString = Myform.rowString.value;
} else {
rowString = undefined;
}

which can be abbreviated to:

rowString = ( MyForm.rowString && MyForm.rowString.value );

I would suggest changing the name to rowValue, but that''s just me.
Reusing variables this way may make maintenance more difficult, but
again, that''s up to you. It may be better to build up the element
name in a variable called ''eleName'', then assign the value to
''eleValue'' so things are a bit clearer and you''d get:

eleValue = ( MyForm.eleName && MyForm.eleName.value );

//alert shows "MyForm.L411.value" - without quotes
//alert(rowString);

if( rowString = "0")
Instead of testing whether the value of ''rowString'' is equivalent to
the string ''0'', you are assigning it the value of the string ''0''.

As a hint always write such tests:

if( ''0'' = rowString )

That way you will get a script error rather than just erroneous
results. The correct syntax (assuming you want to test for equality)
is:

if( ''0'' == rowString )
{
zeroCount++:

}
rowString = "L4" + j;
}
}

How can I test the value of a control, based on a string built?

Many tia''s

ck



-

Rob


--
Rob


ck **** @ fusert.net 写道:
ck****@fusert.net wrote:
我有一个带有40个文本框的表单(默认值为每个= 0)。我想评估每个框以查看该值是否仍为零,如果是,我想将其添加到零计数。盒子名称是L411至L419,L421至L429,L431至L439,L431至L439。和L441至L449。
I have a form with 40 text boxes (default of each = 0). I want to
evaluate each box to see if the value is still zero, and if it is, I
want to add it to a "Zero Count". The box names are "L411 thru L419",
"L421 thru L429", "L431 thru L439" and "L441 thru L449".




(36,而不是40,文本输入)。


var i ,j,nZeroCount,oForm,oCurElem,oCurVal;

oForm = document.forms [''MyForm''];

nZeroCount = 0;

for(i = 1; i< 10; i ++){

for(j = 1; j< 5; j ++){

oCurElem = oForm.elements [ " L4" + j + i];

if(oCurElem){

oCurVal = oCurElem.value;

if(!isNaN(oCurVal)& & parseInt(oCurVal,10)== 0){

nZeroCount ++;

}

}

}

}


ciao,dhgm



(That are 36, not 40, text inputs).

var i, j, nZeroCount, oForm, oCurElem, oCurVal;
oForm = document.forms[''MyForm''];
nZeroCount = 0;
for (i=1; i<10; i++) {
for (j=1; j<5; j++) {
oCurElem = oForm.elements["L4" + j + i];
if (oCurElem) {
oCurVal = oCurElem.value;
if (!isNaN(oCurVal) && parseInt(oCurVal, 10) == 0) {
nZeroCount++;
}
}
}
}

ciao, dhgm


>当我将其插入到If语句中以获取控件的值时,
> When I plug this into an If statement to get the value of the control,
一切都会死亡。这是我的If:
if(rowString =" 0")
everything dies. Here is my If: if( rowString = "0")




如果您使用过JSLint,它会提醒您注意这个语句。你

可能意味着


if(rowString ==" 0"){


http://www.JSLint.com


这篇关于字符串的值???的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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