HTML 提交按钮:不同的值/按钮文本?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4171664/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 05:15:50  来源:igfitidea点击:

HTML Submit-button: Different value / button-text?

htmlforms

提问by aioobe

I'd like to create an HTML form submit button with the value'add tag', however, the web page is in Swedish, so I'd like to have a different button text.

我想用创建一个 HTML 表单提交按钮'add tag',但是,网页是瑞典语,所以我想有一个不同的按钮文本

That is, I want to have a button like

也就是说,我想要一个像

enter image description here

在此处输入图片说明

but I want to have my code like

但我想让我的代码像

if (request.getParameter(cmd).equals("add tag"))
    tags.addTag( /*...*/ );

Is this possible? If so, how?

这可能吗?如果是这样,如何?

回答by Pekka

It's possible using the buttonelement.

可以使用button元素。

<button name="name" value="value" type="submit">S?k</button>

From the W3C page on button:

W3C 页面上button

Buttons created with the BUTTON element function just like buttons created with the INPUT element, but they offer richer rendering possibilities: the BUTTON element may have content.

使用 BUTTON 元素创建的按钮的功能与使用 INPUT 元素创建的按钮一样,但它们提供了更丰富的呈现可能性:BUTTON 元素可能具有内容。

回答by Sony Santos

Following the @greg0ire suggestion in comments:

遵循@greg0ire 在评论中的建议:

<input type="submit" name="add_tag" value="L?gg till tag" />

In your server side, you'll do something like:

在您的服务器端,您将执行以下操作:

if (request.getParameter("add_tag") != null)
    tags.addTag( /*...*/ );

(Since I don't know that language (java?), there may be syntax errors.)

(由于我不知道该语言(java?),可能存在语法错误。)

I would prefer the <button>solution, but it doesn't work as expected on IE < 9.

我更喜欢这个<button>解决方案,但它在 IE < 9 上不能按预期工作

回答by ijw

There are plenty of answers here explaining what you could do (I use the different field name one) but the simple (and as-yet unstated) answer to your question is 'no' - you can't have a different text and value using just HTML.

这里有很多答案解释了您可以做什么(我使用不同的字段名称),但对您的问题的简单(且尚未说明)的答案是“不”-您不能使用不同的文本和值只是 HTML。

回答by Flinsch

I don't know if I got you right, but, as I understand, you could use an additional hidden field with the value "add tag" and let the button have the desired text.

我不知道我是否理解正确,但是,据我所知,您可以使用值为“添加标签”的附加隐藏字段,并让按钮具有所需的文本。

回答by Andrey Pokhilko

If you handle "adding tag" via JScript:

如果您通过 JScript 处理“添加标签”:

<form ...>
<button onclick="...">any text you want</button>
</form>

Or above if handle via page reload

或以上,如果通过页面重新加载处理