引用javascript表单对象 [英] referencing javascript form object
问题描述
我有一个javascript函数如下:
函数myfunction(formName,formField){
parseInt(document.formName.formField.value)+ = 1;
}
然后我可以在需要时调用这个函数:
myfunction(用户) ,用户名);
这是我的问题:
Javascript不评估传递给函数的参数。
相反,它试图使用literarilly的参数。
所以代替这个:
parseInt(document.users.username.value )+ = 1;
javascript实际上是在寻找:
parseInt(document.formName.formField.value)+ = 1;
我也试过以下但没有成功:
parseInt(eval(document.formName.formField).value)+ = 1;
关于如何解决这个问题的任何建议?
phpcode写道:<块引用class =post_quotes>我有一个javascript函数如下:
函数myfunction(formName,formField){
parseInt(document.formName.formField.value)+ = 1;
myfunction(用户,用户名);
这是我的问题:
Javascript不会评估传递给函数的参数。
相反,它试图使用literarilly的参数。
所以不要这样:
parseInt(document.users.username.value)+ = 1;
javascript实际上是在找这个:
parseInt(document.formName.formField.value )+ = 1;
我也尝试了以下但没有成功:
parseInt(eval(document.formName.formField).value)+ = 1;
关于如何解决这个问题的任何建议?
首先阅读小组常见问题解答。
function myFunction(users,username){
parseInt(document.forms [us ers] .elements [username] .value,10)+ = 1;
}
请注意,我在parseInt调用中添加了10的基数。是否
parseInt是工作的最佳工具取决于函数是什么
试图做什么。
-
Randy
comp.lang.javascript常见问题 - http://jibbering.com/faq &新闻组周刊
phpcode说:
我有一个javascript函数如下:
函数myfunction (formName,formField){
parseInt(document.formName.formField.value)+ = 1;
}
然后我可以在需要时调用该函数,如下所示:
myfunction(用户,用户名);
这是我的问题:
Javascript不会评估传递给函数的参数。
而是,它试图使用literarilly的参数。
所以不要这样:
parseInt(document.users.username.value)+ = 1;
javascript实际上是在寻找这个:
parseInt(document.formName.formField.value)+ = 1;
我也试过以下但没有成功:
parseInt(eval(document.formName.formField).valu e)+ = 1;
关于如何解决这个问题的任何建议?
修复你的功能。
它应该参考作为参数的字段。
在引用可用时避免使用名称。
函数myfunction(formFieldReference){
formFieldReference.value ++;
}
并将其命名为:
myfunction(document.formName.formField);
或其他一些参考字段。
兰迪,
谢谢你的答案。
然而,你不明白我的问题。
表格字段名是动态生成的。我不知道它们,并且
无法给出函数的实际值。我可以做的是在调用函数时将它传递给
函数。
然后我希望javascript从传递的参数中提取实名
给它。
换句话说,我做不到
函数myfunction(用户名,用户名){
}
因为users,username每次调用函数时都会更改。
我能做的是在运行时将新值传递给javascript。
I have a javascript function as follows:
function myfunction(formName,formField) {
parseInt(document.formName.formField.value) +=1;
}
Then I can call the function when needed like this:
myfunction(users,username);
Here is my problem:
Javascript does not evaluate the arguments passed to the function.
Instead, it tries to use the arguments literarilly.
So instead of this:
parseInt(document.users.username.value) +=1;
javascript is actually looking for this:
parseInt(document.formName.formField.value) +=1;
I have also tried the following without success:
parseInt(eval(document.formName.formField).value) +=1;
Any suggestions on how to solve this problem?
phpcode wrote:I have a javascript function as follows:
function myfunction(formName,formField) {
parseInt(document.formName.formField.value) +=1;
}
Then I can call the function when needed like this:
myfunction(users,username);
Here is my problem:
Javascript does not evaluate the arguments passed to the function.
Instead, it tries to use the arguments literarilly.
So instead of this:
parseInt(document.users.username.value) +=1;
javascript is actually looking for this:
parseInt(document.formName.formField.value) +=1;
I have also tried the following without success:
parseInt(eval(document.formName.formField).value) +=1;
Any suggestions on how to solve this problem?
You start by reading the group FAQ.
function myFunction(users,username){
parseInt(document.forms[users].elements[username].value,10) += 1;
}
Note that I added the radix of 10 to the parseInt call. And whether
parseInt is the best tool for the job depends on what the function is
attempting to do.
--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
phpcode said:
I have a javascript function as follows:
function myfunction(formName,formField) {
parseInt(document.formName.formField.value) +=1;
}
Then I can call the function when needed like this:
myfunction(users,username);
Here is my problem:
Javascript does not evaluate the arguments passed to the function.
Instead, it tries to use the arguments literarilly.
So instead of this:
parseInt(document.users.username.value) +=1;
javascript is actually looking for this:
parseInt(document.formName.formField.value) +=1;
I have also tried the following without success:
parseInt(eval(document.formName.formField).valu e) +=1;
Any suggestions on how to solve this problem?
Fix your function.
It should take a reference to a field as an argument.
Avoid using names when references are available.
function myfunction(formFieldReference) {
formFieldReference.value++;
}
and call it as:
myfunction(document.formName.formField);
or with some other reference to the field.
Randy,
Thanks for your answer.
However, you did not understand my question.
The form fieldnames are dynamically generated. I do not know them, and
cannot give the function the actual values. What I can do is pass it to
the function while calling the function.
I then want javascript to extract the real name from the arguments passed
to it.
In other words, I cannot do
function myfunction(users,username) {
}
because "users,username" changes each time the function is called.
What I can do is pass the new values to javascript at runtime.
这篇关于引用javascript表单对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!