关于动态创建变量的问题

编程入门 行业动态 更新时间:2024-10-28 10:29:47
本文介绍了关于动态创建变量的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我是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 piecetogether a variable name and then assign it a value. I want to create ahidden field and assign it a value based on the value of another variableRight now it looks like:("document.all.SM_MARK_10" + dateNumber + ".value")where dateNumber is an already defined integer. What I want is to saydocument.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>在消息中写道 新闻:Pv ******************* @ newssvr29.news.prodigy。 com ... "Tom Moroow" <tr**@socal.rr> 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>在消息新闻中写道:< Pv ******************* @ newssvr29.news.prodigy > ... "Tom Moroow" <tr**@socal.rr> wrote in message news:<Pv*******************@newssvr29.news.prodigy >... 我是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>

更多推荐

关于动态创建变量的问题

本文发布于:2023-10-18 03:31:55,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1502956.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:变量   动态

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!