关于动态创建变量的问题 [英] Question on creating variables on the fly

查看:107
本文介绍了关于动态创建变量的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是javascript的新手,想知道如何将变量名称和
组合在一起,然后为它赋值。我想创建一个

隐藏字段,并根据另一个变量的值为其分配值


现在它看起来像:


(" document.all.SM_MARK_10" + dateNumber +" .value")


其中dateNumber是一个已定义的整数。我想要的是说


document.all.SM_MARK_1001.value =" XX"

document.all.SM_MARK_1002.value =" X2"





基于dateNumber的值。有没有办法轻松做到这一点?


Hi, I''m pretty new to javascript and was wondering how you would piece
together a variable name and then assign it a value. I want to create a
hidden field and assign it a value based on the value of another variable

Right now it looks like:

("document.all.SM_MARK_10" + dateNumber + ".value")

where dateNumber is an already defined integer. What I want is to say

document.all.SM_MARK_1001.value="XX"
document.all.SM_MARK_1002.value="X2"

etc.

based on that value of dateNumber. Any way to do this easily?


推荐答案

Tom Moroow说:
Tom Moroow said:
隐藏字段并根据另一个变量的值为其赋值

现在它看起来像:

(文档。 all.SM_MARK_10" + dateNumber +" .value")

其中dateNumber是一个已定义的整数。我想要的是说

document.all.SM_MARK_1001.value =" XX"
document.all.SM_MARK_1002.value =" X2"



基于dateNumber的值。什么方法可以轻松地做到这一点?

Hi, I''m pretty new to javascript and was wondering how you would piece
together a variable name and then assign it a value. I want to create a
hidden field and assign it a value based on the value of another variable

Right now it looks like:

("document.all.SM_MARK_10" + dateNumber + ".value")

where dateNumber is an already defined integer. What I want is to say

document.all.SM_MARK_1001.value="XX"
document.all.SM_MARK_1002.value="X2"

etc.

based on that value of dateNumber. Any way to do this easily?




听起来好像必须有更好的方法来做任何事情

你是实际上试图完成。


一个解决方案可能是在

表单中有两个隐藏字段,名为VARNAME。和VARVALUE,并将您计算的

名称分配给第一个,将值分配给第二个。但是,使用

的名称在您的应用程序中更有意义。


这将有助于了解您想要做什么。表格字段与变量不同,你的术语是




您还应该找到一本新的参考手册教你

你要使用document.all,因为这已经过时了,并且从来没有在所有浏览器上工作过。



It sounds like there must be a better way to do whatever it is
that you''re actually trying to accomplish.

One solution might be to have two hidden fields already in the
form, named "VARNAME" and "VARVALUE", and assign your computed
name to the first and the value to the second. Use names that
are more meaningful in your application, though.

It would help to know what you want to do. Your terminology is
off a bit, since form fields are not the same as variables.

You should also find a new reference manual that doesn''t teach
you to use "document.all", since that is out of date and never
worked on all browsers.




" Tom Moroow" < TR ** @ socal.rr.com>在消息中写道

新闻:Pv ******************* @ newssvr29.news.prodigy。 com ...

"Tom Moroow" <tr**@socal.rr.com> wrote in message
news:Pv*******************@newssvr29.news.prodigy. com...
我是javascript的新手,想知道如何将变量名称组合在一起,然后为其赋值。我想创建一个
隐藏字段并根据另一个变量的值为其赋值

现在它看起来像:

(文档。 all.SM_MARK_10" + dateNumber +" .value")

其中dateNumber是一个已定义的整数。我想要的是说

document.all.SM_MARK_1001.value =" XX"
document.all.SM_MARK_1002.value =" X2"



基于dateNumber的值。有没有办法轻松做到这一点?
Hi, I''m pretty new to javascript and was wondering how you would piece
together a variable name and then assign it a value. I want to create a
hidden field and assign it a value based on the value of another variable

Right now it looks like:

("document.all.SM_MARK_10" + dateNumber + ".value")

where dateNumber is an already defined integer. What I want is to say

document.all.SM_MARK_1001.value="XX"
document.all.SM_MARK_1002.value="X2"

etc.

based on that value of dateNumber. Any way to do this easily?




使用eval(字符串)



Use eval(string)


Tom Moroow < TR ** @ socal.rr.com>在消息新闻中写道:< Pv ******************* @ newssvr29.news.prodigy .com> ...

"Tom Moroow" <tr**@socal.rr.com> wrote in message news:<Pv*******************@newssvr29.news.prodigy .com>...

我是javascript的新手,想知道如何将变量名称组合在一起然后为其赋值。我想创建一个
隐藏字段并根据另一个变量的值为其赋值

现在它看起来像:

(文档。 all.SM_MARK_10" + dateNumber +" .value")

其中dateNumber是一个已定义的整数。我想要的是说

document.all.SM_MARK_1001.value =" XX"
document.all.SM_MARK_1002.value =" X2"



基于dateNumber的值。有什么方法可以轻松地做到这一点吗?
Hi, I''m pretty new to javascript and was wondering how you would piece
together a variable name and then assign it a value. I want to create a
hidden field and assign it a value based on the value of another variable

Right now it looks like:

("document.all.SM_MARK_10" + dateNumber + ".value")

where dateNumber is an already defined integer. What I want is to say

document.all.SM_MARK_1001.value="XX"
document.all.SM_MARK_1002.value="X2"

etc.

based on that value of dateNumber. Any way to do this easily?



这里是你如何拼凑一个Element'的ID而没有硬编码。

确保你使用元素的ID而不是NAME属性。

< input type =" button" ID = QUOT;本身份识别码" value =" Hello"

onClick =" findValue(''ID'')">


< script>

函数findValue(strElementID)

{

//查找并读取值

alert(document.getElementById(''my ''+ strElementID).v alue);


//查找并更改值

document.getElementById(''my''+ strElementID)。 value =" Goodbye";

}

< / script>


Here''s how you piece together an Element''s ID with out hardcoding it.
Make sure you use the element''s ID rather then the NAME attribute.
<input type="button" ID="myID" value="Hello"
onClick="findValue(''ID'')">

<script>
function findValue(strElementID)
{
//Find and Read the value
alert(document.getElementById(''my''+strElementID).v alue);

//Find and Change the value
document.getElementById(''my''+strElementID).value = "Goodbye";
}
</script>


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

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