一个getElementId问题 [英] A getElementId question

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

问题描述

我是js的新手所以这个问题是一个简单的问题,但我还没有能够解决它。


我有2个文本字段,让我选择一个图形文件,然后想要显示2个图像占位符,然后我想调用一个函数

SetImg传递id输入文件和图像名称设置

输入字段前面的文件值的源。这就是我所拥有的

完成


< head>


// Javascript函数

< script type =" text / javascript" language =" javascript">

函数SetImg(fieldIn,imageToSet){

var path = document.getElementById(fieldIn).value;

document.getElementById(imageToSet).scr = path;

}

< / script>

< / head>


< body>

< form name =" form1"方法= QUOT;交" action ="">

< input name =" Browse1"类型= QUOT;文件" ID = QUOT; Browse1" value =""

onchange =" SetImg(Browse1,img1)" />

< input name =" Browse2"类型= QUOT;文件" ID = QUOT; Browse2" value =""

onchange =" SetImg(Browse2,img2)" />

< / form>


< img name =" img1" SRC =""宽度= QUOT; 64"高度= QUOT; 64" alt ="">

< img name =" img2" SRC =""宽度= QUOT; 64"高度= QUOT; 64" alt ="">

< / body>

但是这并没有设置图像。有些请告诉我我在做什么

错误

解决方案



Steven写道:
< blockquote class =post_quotes>
< script type =" text / javascript"语言= QUOT; JavaScript的">



不推荐使用language属性,只使用type属性:


< script type =" text / javascript" ;>


函数SetImg(fieldIn,imageToSet){

var path = document.getElementById(fieldIn).value;

document.getElementById(imageToSet).scr = path;



首先,你有一个拼写错误,应该是src。不是scr:


document.getElementById(imageToSet).src = path;


}


< input name =" Browse1"类型= QUOT;文件" ID = QUOT; Browse1" value =""

onchange =" SetImg(Browse1,img1)" />



getElementById方法需要一个字符串作为参数,你不是
将一个字符串传递给你的函数,因此浏览器应该给你

错误消息。要解决这个问题,你应该像这样发送你的字符串(注意

单引号和双引号):


onchange =" SetImg('' Browse1'',''img1'')"


< img name =" img1" SRC =""宽度= QUOT; 64"高度= QUOT; 64" alt ="">

< img name =" img2" SRC =""宽度= QUOT; 64"高度= QUOT; 64" ALT ="">



在您的函数中,您尝试使用

getElementById方法获取对图像的引用。但是,你的图像元素开头没有

id,因此无论如何该方法都会失败。


但这不是''设置图像。可以请告诉我我在做什么

错误



从上面的建议中获取。你可以做的另一件事是,

而不是使用getElementById方法来获取对你的

图像的引用,你也可以使用document.images集合。例如:


document.images [" img1"]。src = path;




web.dev写道:


不推荐使用language属性,只使用type属性:

< script type =" text / javascript">



更正


首先,你有一个拼写错误,应该是src。不是scr:


document.getElementById(imageToSet).src = path;



更正


onchange =" SetImg(''Browse1'',''img1'' )"



更正


>


< img name =" img1" SRC =""宽度= QUOT; 64"高度= QUOT; 64" alt ="">

< img name =" img2" SRC =""宽度= QUOT; 64"高度= QUOT; 64" ALT ="">



在您的函数中,您尝试使用

getElementById方法获取对图像的引用。但是,您的图像元素开头没有

id,因此该方法无论如何都会失败。



试过以下内容

< img name =" img1" ID" IMG1" SRC =""宽度= QUOT; 64"高度= QUOT; 64" alt ="">

< img name =" img2" ID" IMG2" SRC =""宽度= QUOT; 64"高度= QUOT; 64" alt ="">


没有成功


从上面获取建议。你可以做的另一件事是,

而不是使用getElementById方法来获取对你的

图像的引用,你也可以使用document.images集合。例如:


document.images [" img1"]。src = path;



试过这个但是仍然没有成功(这也意味着img1是

硬编码,因为我有大约20张图片设置那就是为什么我要尝试

将图像传递给函数?)


另外我把

警告(路径);行后

var path = document.getElementById(''fieldIn'')。value;


但它从未显示


Steven在8/21/2006 8:16 PM说了以下内容:


你好我是新来的js所以这个问题是一个简单的问题,但我还没有能够解决它。



如果文件在本地运行,你可以解决它。如果是从服务器运行

那么你就无法解决它。


我有2个文本字段让我选择一个图形文件,然后想要显示2个图像占位符,然后我想调用一个函数

SetImg传递输入文件的id和图像名称将输入字段的文件值设置为

。这就是我所拥有的

完成


< head>


// Javascript函数

< script type =" text / javascript" language =" javascript">

函数SetImg(fieldIn,imageToSet){

var path = document.getElementById(fieldIn).value;



为什么要使用gEBI来获取表单输入的值?使用更多

广泛支持的表单集合。但是,如果你正确编码,你甚至不需要参考文件输入




< script type =" text / javascript">

函数showImage(imagePath,imageName){

document.images [imageName] .src = imagePath;

}

< / script>

< img src =" blank.gif" name =" myImage">

< input type =" file" onchange =" showImage(this.value,''myImage'')">

< img src =" blank.gif" name =" myImage2">

< input type =" file" onchange =" showImage(this.value,''myImage2'')">


只需更改以继续添加更多内容即图片名称和

函数调用的第二个参数。为什么要比它更难?

必须是?

-

Randy

comp.lang.javascript常见问题 - http://jibbering.com/faq &新闻组每周

Javascript最佳实践 - http://www.JavascriptToolbox .com / bestpractices /


Hi I''m new to js so this prob is a simple question but I haven''t been
able to solve it.

I have 2 text fields which let me pick a graphic file and want to
display then in 2 image place holders so I want to call a function
SetImg passing the id of the input file and the image name to to set
the source to the file value fron the input field. Here is what I have
done

<head>

//Javascript function
<script type="text/javascript" language="javascript">
function SetImg(fieldIn, imageToSet) {
var path = document.getElementById(fieldIn).value;
document.getElementById(imageToSet).scr = path;
}
</script>
</head>

<body>
<form name="form1" method="post" action="">
<input name="Browse1" type="file" id="Browse1" value=""
onchange="SetImg(Browse1, img1)"/>
<input name="Browse2" type="file" id="Browse2" value=""
onchange="SetImg(Browse2, img2)"/>
</form>

<img name="img1" src="" width="64" height="64" alt="">
<img name="img2" src="" width="64" height="64" alt="">
</body>
But this doesn''t set the image.Can some please tell me what I''m doing
wrong

解决方案


Steven wrote:

<script type="text/javascript" language="javascript">

The language attribute is deprecated, use just the type attribute:

<script type = "text/javascript">

function SetImg(fieldIn, imageToSet) {
var path = document.getElementById(fieldIn).value;
document.getElementById(imageToSet).scr = path;

First, you have a misspelling, should be "src" not "scr":

document.getElementById(imageToSet).src = path;

}

<input name="Browse1" type="file" id="Browse1" value=""
onchange="SetImg(Browse1, img1)"/>

The getElementById method expects a string as an argument, you are not
passing a string to your function, thus the browser should give you an
error message. To fix this, you should send your strings like so (note
the single quotes and the double quotes):

onchange = "SetImg(''Browse1'', ''img1'')"

<img name="img1" src="" width="64" height="64" alt="">
<img name="img2" src="" width="64" height="64" alt="">

In your function, you try to get a reference to your images by using
the getElementById method. However, your image elements do not have an
id to begin with, therefore the method will fail anyway.

But this doesn''t set the image.Can some please tell me what I''m doing
wrong

Take the suggestions from above. Another thing you can do instead is,
instead of using getElementById method to get a reference to your
images, you can also use the document.images collection. For example:

document.images["img1"].src = path;



web.dev wrote:

The language attribute is deprecated, use just the type attribute:

<script type = "text/javascript">

Corrected

First, you have a misspelling, should be "src" not "scr":

document.getElementById(imageToSet).src = path;

Corrected

onchange = "SetImg(''Browse1'', ''img1'')"

Corrected

>

<img name="img1" src="" width="64" height="64" alt="">
<img name="img2" src="" width="64" height="64" alt="">


In your function, you try to get a reference to your images by using
the getElementById method. However, your image elements do not have an
id to begin with, therefore the method will fail anyway.

Tried the following
<img name="img1" id"img1" src="" width="64" height="64" alt="">
<img name="img2" id"img2" src="" width="64" height="64" alt="">

No success

Take the suggestions from above. Another thing you can do instead is,
instead of using getElementById method to get a reference to your
images, you can also use the document.images collection. For example:

document.images["img1"].src = path;

Tried this also but still no success (also does this mean "img1" is
hard coded and as I have approx 20 images to set thats why I''m trying
to pass the image to the function?)

Also I put
alert(path); after the line
var path = document.getElementById(''fieldIn'').value;

but it never shows


Steven said the following on 8/21/2006 8:16 PM:

Hi I''m new to js so this prob is a simple question but I haven''t been
able to solve it.

If the file is run locally, you can solve it. If it is run from a server
then you won''t be able to solve it.

I have 2 text fields which let me pick a graphic file and want to
display then in 2 image place holders so I want to call a function
SetImg passing the id of the input file and the image name to to set
the source to the file value fron the input field. Here is what I have
done

<head>

//Javascript function
<script type="text/javascript" language="javascript">
function SetImg(fieldIn, imageToSet) {
var path = document.getElementById(fieldIn).value;

Why use gEBI at all to get the value of a form input? Use the more
widely supported forms collection. But, you don''t even need a reference
to the file input if you code it correctly:

<script type="text/javascript">
function showImage(imagePath,imageName){
document.images[imageName].src = imagePath;
}
</script>
<img src="blank.gif" name="myImage">
<input type="file" onchange="showImage(this.value,''myImage'')">
<img src="blank.gif" name="myImage2">
<input type="file" onchange="showImage(this.value,''myImage2'')">

Only thing to change to keep adding more is the name of the image and
the second parameter to the function call. Why make it harder than it
has to be?
--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/


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

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