16

单击按钮时,我正在使用 jQuery 提交表单。然后我想测试按钮的值,但是,这不会在 Safari 和 Chrome 中提交。为什么?有解决办法吗?

在 IE/FF 中测试下面的代码看它是否有效,然后在 Safari/Chrome 中测试它是否无效:

ButtonPostTest.php:

<?php
    if(isset($_POST['testBtn'])) {
        echo $_POST['testBtn'];
    }
?>
<html>
    <head>
    <title></title>
        <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
        <script type="text/javascript">
            $(document).ready(function() {
                $('#btn').click(function() {
                    $('form').submit();  // submit the form
                });
            });
        </script>
    </head>
    <body>
        <form action="" method="post">          
            <button id="btn" class="page-btn" type="submit" name="testBtn" value="Yes Button value is posted!">POST IT!</button>
        </form>
    </body>
</html>

更新:

同样的事情发生在input

<input id="btn" class="page-btn" type="submit" name="testBtn" value="Post this"></input>

此外,这只是使用 jQuery 提交时的一个问题.submit()- 没有这个,它可以通过按钮或输入很好地提交。

4

10 回答 10

7

给它一个名字。

<button type='submit' id='trash' name='trash' value="trash" class='red'>
于 2013-08-31T16:01:29.163 回答
2

我想你已经发现了一个 webkit 错误。

我在 Chrome 中进行了同样的测试,您的表单:

<form action="" method="post">          
   <button id="btn" class="page-btn" type="submit" name="testBtn" value="Yes Button value is posted!">POST IT!</button>
</form>

这不发送任何数据。但是,如果我添加另一个元素,特别是输入:

<form action="" method="post">          
   <input type="hidden" name="shim" value="" />
   <button id="testBtn" class="page-btn" type="submit" name="testBtn" value="Yes Button value is posted!">POST IT!</button>
</form>

这会发送两个值。这没有 JavaScript。一旦你添加了 shim 隐藏表单元素,它看起来就变得有意义了。

此解决方案需要更多测试,但它可能会满足您的需求。

于 2012-07-08T06:15:14.463 回答
2

正如罗伯特上面所说,在使用<button>标签时,您需要两者name=""value="",例如

<button name="Search" value="Simple" type="submit">Search</button>

VS

<button name="Search" value="Advanced" type="submit">Search</button>

在此示例中,您的 POST 数据将具有以下键值

搜索简单

或者

高级搜索

然后基于该 POST 值,您可以编写代码以采取适当的措施。:)

即您不需要使用输入,只需使用带有名称/值的按钮就可以了。

于 2013-10-30T03:06:28.747 回答
1

这可能是非常明显的,现在我自己修复了它,我意识到这是一个愚蠢的错误。但对我来说,我错误地离开了type="submit"。表单始终有效,但只是从这些按钮中发布了一个值。

未传递值(错误):

<button name="step" id="btn1" value="my_value">btn_text</button>

价值通过(好):

<button type="submit" name="step" id="btn1" value="my_value">btn_text</button>

无论如何,我希望这对刚开始的新人有所帮助,或者坚持一些看起来如此明显的事情。

干杯!

于 2018-05-21T14:08:35.213 回答
1

所以,我有一件非常相似的事情发生在我身上。一切正常,除了 Chrome [版本 78.0.3904.108 (Official Build) (64-bit)]。

当我取出 <button> 中的 id= 时,它起作用了!但这不是问题所在。问题原来是我在 <button> 标签中的 onclick= 处禁用了 [Submit] 按钮(使用按钮 id=)。执行此操作时,Chrome 似乎不会发布任何内容。哪个有点无意义?而且很奇怪?我不知道为什么?

所以我花了很多时间挖掘,发现你可以在 <form> 中的 onsubmit= 处禁用 [Submit] 按钮。事实上,onsubmit= 与 onclick= 几乎相同,所以在未来我认为我需要改变一些东西,以便我始终使用 onsubmit=,然后所有大型浏览器都将继续为我工作。

于 2019-12-04T01:52:56.260 回答
0

同意 Vins,我可能会使用<input>.

<button>您使用而不是有特殊原因<input>吗?我通常更喜欢<button>(允许我添加图标和图像),但我相信浏览器之间在如何解释值方面存在差异......

根据w3schools Mozilla Developer Network的说法,一些浏览器会提交 value 属性,而其他浏览器如 IE7 会错误地提交标签之间的文本。

于 2012-07-08T05:32:08.503 回答
0

尝试像这样通过捕获隐藏的输入来发送按钮作为提交。

PHP:

<?php
    if(isset($_POST['btname']))
        echo $_POST['btname'];
    }
 ?>

和html:

<form method="POST" action="" id="formname" name="formname">
<input name="btname" id="btname" type="hidden" />
<button onclick="document.formname.submit()" value="Submit">Send</button>
</form>
于 2013-12-05T02:32:26.267 回答
0

<button>并且<input>是两个不同的标签。如果要提交按钮值,请使用<input>标签。

于 2012-07-08T05:20:10.610 回答
0

How many values are you POSTing?

I'm not sure it sounds like your problem was solved. One thing that has not been mentioned is that you may have overshot your php max_input_vars limit.

Php is silent when you overshoot this limit, so you'll have to check the warning log, or check it this way:

1. Count the POST array values. Scripting in var_dump($_POST); (in a safe production sandbox environment) will do this best, as you can look at the final value output to see where the POST array "cuts off".

2. Next, run a script to output the php information using the function: phpinfo(). Check the value of "max_input_vars".

3. If the POST array count == the "max_input_vars" value, then you have overshot the limit.

In your case, it may be the case that the submit input would have been included later in the POST.

To edit the value (to 5000 in this example) in php.ini, add or edit this line: max_input_vars = 5000

于 2017-11-27T17:33:24.973 回答
0

提交/单击事件期间禁用的按钮不会提交其值

根据 Alan Stewart 的说法,当我编写一个小实用程序来防止双击一个表单提交按钮时,我遇到了这个问题,该按钮监听了“提交”并禁用了该按钮。

我更改了我的代码,因此它确定表单是否已经提交,如果是,则禁用按钮并阻止表单通过event.preventDefault().

它不像以前那么好立即禁用提供即时视觉反馈的按钮(因为 Bootstrap 具有禁用按钮的样式)。

于 2020-11-05T16:49:20.897 回答