< button type =" submit">兼容性? [英] <button type="submit"> compatibility?

查看:75
本文介绍了< button type =" submit">兼容性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想提交提交按钮的提交按钮不同的值。使用< input type =submit> ,您似乎无法做到这一点。然而,使用< button type =submit> ,这些可以是两个不同的值。问题是,它会在所有浏览器中工作吗?



试试这个测试代码:

 < form method =getaction => 
< input type =textname =txt/>
< button type =submitname =btnvalue =val>文字< / button>
< / form>

在FF 3.6中,它会更新我的地址栏,并且两个值都相应(并按回车键文本框)。在IE 8中,它也接受按下回车键,在地址栏中显示文本值,但它并没有将按钮的值显示为GET参数......这是否意味着它没有提交它?






我不能使用隐藏输入,因为我需要确定哪个按钮被点击而没有JS。






测试2:

 < form method =get action => 
< input type =textname =txt/>
< button type =submitname =submit1value =submit1> submit< / button>
< input type =submitname =submit2value =submit2/>
< input type =submitname =submit3value =submit3/>
< / form>

在IE8中,按Enter键不会提交任何按钮,但是单击submit1 发送一个值。它会发送提交,而不是与FF不一致的submit1。但是,提交表单只会在两个浏览器中发送 one 按钮的值,这意味着我可以通过检查哪个按钮被点击> GET ['submitX'] 改为存在! Chrome在按下回车时的行为略有不同(提交按钮2)。歌剧似乎与FF一致...但所有4个浏览器只提交一个按钮。我没有安装早期版本的浏览器,但是......有人知道它是否适用于早期版本,尤其是IE6?

解决方案 div>

Oldish的帖子,但我认为有一个解决方案,这里已经错过了。 按钮[type = submit] 一直存在行为不一致的问题,特别是对于旧的IE,并且@ Mark的测试看起来像我们仍然有问题。 b
$ b

相反,您可以对输入的名称属性使用两个不同的值输入[type = submit] 并解析这些服务器端以获得正确的操作。例如,您可以这样做:

 < form method =getaction => 
< input type =textname =txt/>
< input type =submitname =action [do_something]value =Do Something Cool/>
< input type =submitname =action [do_another]value =Do Something Dangerous/>
< / form>

只有成功的(点击或默认)名称/值对才会被提交,例如在PHP中可以很容易地做到这一点:

 <?php 
if(isset($ _ GET ['action'] [ 'do_something'])){
//做某事
} else {
//做另一件事
}
?>

请注意,默认操作始终是出现在源代码中的第一个。


I'd like to have a submit button that submits a different value than is displayed on the button. With <input type="submit"> you can't seem to do this. With <button type="submit"> however, these can be two different values. The question is, will it work in all browsers?

Trying this test code here:

<form method="get" action="">
    <input type="text" name="txt"/>
    <button type="submit" name="btn" value="val">text</button>
</form>

In FF 3.6 it updates my address bar with both values appropriately (and responds to me pressing enter in the text box). In IE 8, it also accepts pressing enter, displays the text value in the address bar, but it doesn't show the button's value as a GET param at all... does that mean it's not submitting it?


I can't use hidden inputs because I need to determine which button is clicked without JS.


Test 2:

<form method="get" action="">
    <input type="text" name="txt"/>
    <button type="submit" name="submit1" value="submit1">submit</button>
    <input type="submit" name="submit2" value="submit2"/>
    <input type="submit" name="submit3" value="submit3"/>
</form>

In IE8, hitting enter does not submit any of the buttons, but clicking submit1 will send a value. It'll send "submit", not "submit1" which is inconsistent with FF. However, submitting the form only sends the value of one button in both browsers, which means I might be able to check which button was clicked by checking if GET['submitX'] exists instead! Chrome has slightly different behavior on pressing enter (submits button2). Opera seems consistent with FF... but all 4 browsers only ever submit one button. I don't have any earlier versions of the browsers installed though.... does anyone know if it works in earlier versions, particularly IE6?

解决方案

Oldish post but I think there is a solution that has been missed here. The button[type=submit] has always had problems with inconsistent behaviour, particularly with old IEs, and from @Mark's tests looks like we still have problems.

Instead you can use two different values for the name attribute of the input[type=submit] and parse these server-side to get the correct action. For example, you can do:

<form method="get" action="">
    <input type="text" name="txt"/>
    <input type="submit" name="action[do_something]" value="Do Something Cool"/>
    <input type="submit" name="action[do_another]" value="Do Something Dangerous"/>
</form>

Only the successful (clicked or default) name-value pair gets submitted so, say, in PHP you can easily do something like:

<?php
if (isset($_GET['action']['do_something'])) {
    // do something
} else {
    // do another thing
}
?>

Note the default action will always be the first that appears in the source.

这篇关于&lt; button type =&quot; submit&quot;&gt;兼容性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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