使用javascript设置表单项值 [英] set form item value with javascript

查看:69
本文介绍了使用javascript设置表单项值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在寻找答案,我必须

搜索错误的关键字。


以下是一个片段我的形式。还有其他表单项,但是我需要根据点击的图像提交不同的值。

让我们说名称是图像。值为[1,2,3]。


如何使用javascript提交?


< form name =" ; form1中"方法= QUOT;交" action =" launch.asp">

< A HREF =" javascript :void();" ONCLICK = QUOT; document.form1.submit();返回

false;">< IMG NAME =" image1" SRC =" image1.jpg">< / A>


< A HREF =" javascript :void();" ONCLICK = QUOT; document.form1.submit();返回

false;">< IMG NAME =" image2" SRC =" image2.jpg">< / A>


< A HREF =" javascript :void();" ONCLICK = QUOT; document.form1.submit();返回

false;">< IMG NAME =" image3" SRC =" image3.jpg">< / A>

< / form>


谢谢,

Brian

I have been searching for a while to find an answer to this and I must
be searching on the wrong keywords.

Below is a snippet of my form. There are other form items on it, but I
need to submit a different value based on the image that is clicked.
Lets say the name is "image" and the value is [1,2,3].

How do I submit this with javascript?

<form name="form1" method="post" action="launch.asp">
<A HREF="javascript: void();" ONCLICK="document.form1.submit(); return
false;"><IMG NAME="image1" SRC="image1.jpg"></A>

<A HREF="javascript: void();" ONCLICK="document.form1.submit(); return
false;"><IMG NAME="image2" SRC="image2.jpg"></A>

<A HREF="javascript: void();" ONCLICK="document.form1.submit(); return
false;"><IMG NAME="image3" SRC="image3.jpg"></A>
</form>

Thanks,
Brian

推荐答案

Brian D写道:
Brian D wrote:
我一直在寻找答案这个,我必须搜索错误的关键字。

以下是我的表格的片段。还有其他表单项,但我需要根据点击的图像提交不同的值。
让我们说名称是图像。值为[1,2,3]。

如何用javascript提交?


使用JavaScript提交表单被认为是不好的做法。这使得

表格对于禁用JavaScript的任何人来说都是无用的。

可用。对于文本或非可视浏览器,它也不是很容易访问。

< form name =" form1"方法= QUOT;交" action =" launch.asp">
< A HREF =" javascript :void();" ONCLICK = QUOT; document.form1.submit();返回
false;">< IMG NAME =" image1" SRC =" image1.jpg">< / A>


A元素应该有一个href属性可以做一些有用的事情 -

就像链接到一个页面那样做了什么会发生什么事情

用户可以使用JavaScript。如果这是用于内联网并且你没有打算这样做,请删除A元素,将onclick放在图像上

并使用CSS来制作游标当它在图像上方时指针:


光标:指针;


一般规则是你的页面应该在没有JavaScript的情况下工作。

可以添加脚本以使拥有它的人更轻松,并且

永远不会导致无法访问的界面。

< A HREF =" javascript :void();" ONCLICK = QUOT; document.form1.submit();返回
false;">< IMG NAME =" image2" SRC =" image2.jpg">< / A>

< A HREF =" javascript :void();" ONCLICK = QUOT; document.form1.submit();返回
false;">< IMG NAME =" image3" SRC =" image3.jpg">< / A>
< / form>

谢谢,
Brian
I have been searching for a while to find an answer to this and I must
be searching on the wrong keywords.

Below is a snippet of my form. There are other form items on it, but I
need to submit a different value based on the image that is clicked.
Lets say the name is "image" and the value is [1,2,3].

How do I submit this with javascript?
Using JavaScript to submit a form is considered bad practice. It makes
the form totally usless to anyone with JavaScript disabled or not
available. It is also not very accessible for text or non-visual browsers.

<form name="form1" method="post" action="launch.asp">
<A HREF="javascript: void();" ONCLICK="document.form1.submit(); return
false;"><IMG NAME="image1" SRC="image1.jpg"></A>
The A element should have an href attribute that does something useful -
like linking to a page that does whatever would have happened had the
user had JavaScript available. If this is for an intranet and you don''t
intend doing that, remove the A element, put the onclick on the image
and use CSS to make the cursor a pointer when it''s over the image:

cursor: pointer;

The general rule is that your pages should work without JavaScript.
Scripting can be added to make life easier for those who have it and
should never result in an inaccessible interface.

<A HREF="javascript: void();" ONCLICK="document.form1.submit(); return
false;"><IMG NAME="image2" SRC="image2.jpg"></A>

<A HREF="javascript: void();" ONCLICK="document.form1.submit(); return
false;"><IMG NAME="image3" SRC="image3.jpg"></A>
</form>

Thanks,
Brian



<!DOCTYPE HTML PUBLIC" - // W3C // DTD HTML 4.01 // EN"

" http://www.w3.org/TR/html4/strict.dtd" >

< html>

< head>

< title>新文件< / title>

< meta http-equiv =" Content-Type"

content =" text / html; charset = ISO-8859-1">

< meta name =" generator" content =" editplus">


< style type =" text / css">

/ *如果从form1中删除了一个元素,则需要* /

#form1 img {cursor:pointer; }

< / style>


< script type =" text / javascript">

function submitForm( i){

var f = i.parentNode;

while(f.parentNode&&''form''!= f.nodeName.toLowerCase()) {

f = f.parentNode;

}

f.elements [''imageClickedOn'']。value = i.name;

f.submit();

}

< / script>


< / head> ;

< body>


< form name =" form1"行动= QUOT;" id =" form1">

< a href =" image1clickedOn.html">< img name =" image1" src =" image1.gif"

alt ="点击提交 onclick =" submitForm(this); return false;">

< a href =" image2clickedOn.html">< img name =" image2" src =" image2.gif"

alt ="点击提交 onclick =" submitForm(this); return false;">

< a href =" image3clickedOn.html">< img name =" image3" src =" image3.gif"

alt ="点击提交 onclick =" submitForm(this); return false;">

< input type =" hidden"名称= QUOT; imageClickedOn" value =" none">

< / form>


< / form>

< / body> ;

< / html>

-

Rob


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title> new document </title>
<meta http-equiv="Content-Type"
content="text/html; charset=ISO-8859-1">
<meta name="generator" content="editplus">

<style type="text/css">
/* Needed if A element removed from form1 */
#form1 img { cursor: pointer; }
</style>

<script type="text/javascript">
function submitForm( i ) {
var f = i.parentNode;
while ( f.parentNode && ''form'' != f.nodeName.toLowerCase() ){
f = f.parentNode;
}
f.elements[''imageClickedOn''].value = i.name;
f.submit();
}
</script>

</head>
<body>

<form name="form1" action="" id="form1">
<a href="image1clickedOn.html"><img name="image1" src="image1.gif"
alt="Click to submit" onclick="submitForm(this);return false;">
<a href="image2clickedOn.html"><img name="image2" src="image2.gif"
alt="Click to submit" onclick="submitForm(this);return false;">
<a href="image3clickedOn.html"><img name="image3" src="image3.gif"
alt="Click to submit" onclick="submitForm(this);return false;">
<input type="hidden" name="imageClickedOn" value="none">
</form>

</form>
</body>
</html>
--
Rob


Rob,


谢谢。那很有效。如果通过javascript做这件事很糟糕,那我该怎么办呢?b $ b我这样做了吗?


Brian

Rob,

Thanks. That worked. If doing this via javascript is bad, how whould
I do it otherwise?

Brian


RobG写道:
< script type =" text / javascript">
function submitForm(i){
var f = i.parentNode;


如果浏览器不说JS DOM? ?

为什么不使用表格树?


< ziuup>

< form name =" form1"行动= QUOT;" id =" form1">
< a href =" image1clickedOn.html">< img name =" image1" src =" image1.gif"
alt ="点击提交 onclick =" submitForm(this); return false;">


与表单树兼容:

< script type =" text / javascript">

函数submitForm (i){document.forms [i] .submit(); }

< / script>

onclick =" submitForm(''form1''); return false;">


和/或无需解释您所谈论的父母

onclick =" document.forms [''form1'']。submit();"


和那:

alt ="点击提交很beurk!和IE浏览器使用方式不好


alt ="提交按钮" title ="点击提交


< cite>对于文本或非可视浏览器来说,它也不是很容易访问。
<script type="text/javascript">
function submitForm( i ) {
var f = i.parentNode;
and if browser doesn''t speak JS DOM ? ?
why not use forms tree ?

<ziuup>
<form name="form1" action="" id="form1">
<a href="image1clickedOn.html"><img name="image1" src="image1.gif"
alt="Click to submit" onclick="submitForm(this);return false;">
to be compatible with forms tree :
<script type="text/javascript">
function submitForm( i ) { document.forms[i].submit(); }
</script>
onclick="submitForm(''form1'');return false;">

and/or no need to explain which parent you talk about
onclick="document.forms[''form1''].submit();"

and that :
alt="Click to submit" very beurk ! and bad IE way of use

alt="Submit button" title="Click to submit"

<cite> It is also not very accessible for text or non-visual browsers.



< / cite>


我认为被捣乱的链接是不好的方式要做什么?

所以没有链接

< img name =" image1" SRC =" image1.gif" alt ="提交按钮"

title ="点击提交" onclick =" submitForm(''form1'')">

或带按钮

< input type =" button" name =" image1"

style =" background:url(image1.gif); width:100px; heig ht:20px"

title ="点击进入提交" onclick =" this.form.submit()">

或(为什么不使用的是什么)提交按钮:

<输入类型= [提交" name =" image1"

style =" background:url(image1.gif); width:100px; heig ht:20px"

title ="点击进入提交"值= QUOT; ">

最后一张没有在Safari中显示背景图片

-

Stephane Moriaux et son [moins] vieux Mac


</cite>

I thought squizzed link was bad way to do ?
so with no link
<img name="image1" src="image1.gif" alt="Submit button"
title="Click to submit" onclick="submitForm(''form1'')">
or with a button
<input type="button" name="image1"
style="background:url(image1.gif);width:100px;heig ht:20px"
title="Click to submit" onclick="this.form.submit()">
or (why not to use what is made for) a submit button :
<input type="submit" name="image1"
style="background:url(image1.gif);width:100px;heig ht:20px"
title="Click to submit" value=" ">
this last one doesn''t show background image in Safari
--
Stephane Moriaux et son [moins] vieux Mac


这篇关于使用javascript设置表单项值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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