jquery - 使用 btn-group 时,单选按钮值未正确发送

标签 jquery twitter-bootstrap radio-button

当我使用 btn-group 和最新的 Twitter Bootstrap 显示精美的单选按钮时,提交的值始终与屏幕上标记的值相同。问题是浏览器将这些框标记为已选中(它添加了 active 类),但实际元素仍未选中。

在 Firefox 和 Chrome 中都会出现此问题。它并不总是发生,但大约有 7% 的时间发生(测试了 150 次)。我单击一堆按钮来标记所有分数为 5 的元素,它看起来像这样:

All marked with score 5

但是当我提交并显示结果时,您可以看到它发送的内容不一样!

Array with results, not the same

这种行为不稳定且不可预测,增加了我的困惑,但它是可重现的,只是不会一直发生。要查看,请尝试两个选项之一。 KyleMit非常友好,以至于将其变成了 jsfiddle 。请尝试一下那个。提交表单大约 30 次(每次提交时更改值),您就会遇到错误。我在下面显示了屏幕截图。

第二个选项是运行下面的代码,它有更多的输入字段。更改所有字段,它们的结果将不一样。大约尝试 6 次后会出现错误。

<?php
if ($_POST) {
  var_dump($_POST['quality']);
}
?>
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <link href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css" rel="stylesheet" />
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    <script src="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"></script>
    <title>Test</title>
    <style type="text/css">
        .btn { opacity:1 }
    </style>
  </head>
  <body>
    <div class="row">
      <div class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main">
        <form method="post" class="form form-horizontal">
          <fieldset>
            <legend>Quality</legend>
            <div class="row">
              <div class="col-sm-3">Test 1</div>
              <div class="col-sm-9">
                <div class="btn-group" data-toggle="buttons">
                    <label class="tooltip btn btn-default">
                        <input type="radio" id="q156" name="quality[25]" value="1" /> 1
                    </label> 
                    <label class="tooltip btn btn-default">
                        <input type="radio" id="q157" name="quality[25]" value="2" /> 2
                    </label> 
                    <label class="tooltip btn btn-default">
                        <input type="radio" id="q158" name="quality[25]" value="3" /> 3
                    </label> 
                    <label class="tooltip btn btn-default">
                    <input type="radio" id="q159" name="quality[25]" value="4" /> 4
                        </label> 
                    <label class="tooltip btn btn-default active">
                        <input type="radio" id="q160" name="quality[25]" checked="checked" value="5" /> 5
                    </label>
                </div>
              </div>
            </div>
            <div class="row">
              <div class="col-sm-3">Test 2</div>
              <div class="col-sm-9">
                <div class="btn-group" data-toggle="buttons">
                    <label class="tooltip btn btn-default">
                        <input type="radio" id="q128" name="quality[21]" value="1" /> 1
                    </label> 
                    <label class="tooltip btn btn-default active">
                        <input type="radio" id="q129" name="quality[21]" checked="checked" value="2" /> 2
                    </label> 
                    <label class="tooltip btn btn-default">
                        <input type="radio" id="q130" name="quality[21]" value="3" /> 3
                    </label> 
                    <label class="tooltip btn btn-default">
                        <input type="radio" id="q131" name="quality[21]" value="4" /> 4
                    </label> 
                    <label class="tooltip btn btn-default">
                        <input type="radio" id="q132" name="quality[21]" value="5" /> 5
                    </label>
                </div>
              </div>
            </div>
            <div class="row">
              <div class="col-sm-3">Test 3</div>
              <div class="col-sm-9">
                <div class="btn-group" data-toggle="buttons">
                    <label class="tooltip btn btn-default">
                        <input type="radio" id="q149" name="quality[24]" value="1" /> 1
                    </label> 
                    <label class="tooltip btn btn-default">
                        <input type="radio" id="q150" name="quality[24]" value="2" /> 2
                    </label> 
                    <label class="tooltip btn btn-default">
                        <input type="radio" id="q151" name="quality[24]" value="3" /> 3
                    </label> 
                    <label class="tooltip btn btn-default">
                        <input type="radio" id="q152" name="quality[24]" value="4" /> 4
                    </label> 
                    <label class="tooltip btn btn-default active">
                        <input type="radio" id="q153" name="quality[24]" checked="checked" value="5" /> 5
                    </label>
                </div>
              </div>
            </div>
            <div class="row">
              <div class="col-sm-3">Test 4</div>
              <div class="col-sm-9">
                <div class="btn-group" data-toggle="buttons">
                    <label class="tooltip btn btn-default">
                        <input type="radio" id="q142" name="quality[23]" value="1" /> 1
                    </label> 
                    <label class="tooltip btn btn-default active">
                        <input type="radio" id="q143" name="quality[23]" checked="checked" value="2" /> 2
                    </label> 
                    <label class="tooltip btn btn-default">
                        <input type="radio" id="q144" name="quality[23]" value="3" /> 3
                    </label> 
                    <label class="tooltip btn btn-default">
                        <input type="radio" id="q145" name="quality[23]" value="4" /> 4
                    </label> 
                    <label class="tooltip btn btn-default">
                        <input type="radio" id="q146" name="quality[23]" value="5" /> 5
                    </label>
                </div>
              </div>
            </div>
            <div class="row">
              <div class="col-sm-3">Test 5</div>
              <div class="col-sm-9">
                <div class="btn-group" data-toggle="buttons">
                    <label class="tooltip btn btn-default">
                        <input type="radio" id="q121" name="quality[20]" value="1" /> 1
                    </label> 
                    <label class="tooltip btn btn-default active">
                        <input type="radio" id="q122" name="quality[20]" checked="checked" value="2" /> 2
                    </label> 
                    <label class="tooltip btn btn-default">
                        <input type="radio" id="q123" name="quality[20]" value="3" /> 3
                    </label> 
                    <label class="tooltip btn btn-default">
                        <input type="radio" id="q124" name="quality[20]" value="4" /> 4
                    </label> 
                    <label class="tooltip btn btn-default">
                        <input type="radio" id="q125" name="quality[20]" value="5" /> 5
                    </label>
                </div>
              </div>
            </div>
            <div class="row">
              <div class="col-sm-3">Test 6</div>
              <div class="col-sm-9">
                <div class="btn-group" data-toggle="buttons">
                    <label class="tooltip btn btn-default">
                        <input type="radio" id="q135" name="quality[22]" value="1" /> 1
                    </label> 
                    <label class="tooltip btn btn-default">
                        <input type="radio" id="q136" name="quality[22]" value="2" /> 2
                    </label> 
                    <label class="tooltip btn btn-default">
                        <input type="radio" id="q137" name="quality[22]" value="3" /> 3
                    </label> 
                    <label class="tooltip btn btn-default">
                        <input type="radio" id="q138" name="quality[22]" value="4" /> 4
                    </label> 
                    <label class="tooltip btn btn-default active">
                        <input type="radio" id="q139" name="quality[22]" checked="checked" value="5" /> 5
                    </label>
                </div>
              </div>
            </div>
          </fieldset>
          <button type="submit" class="btn btn-primary">Save</button>
        </form>
      </div>
    </div>
  </body>
</html>

fiddle 结果:

Submit value 5, received differently

最佳答案

Solved !解决方案:

[data-toggle="buttons"] > .btn > input[type="radio"],
[data-toggle="buttons"] > .btn > input[type="checkbox"] {
    visibility: hidden;
}

事实证明,只有当您单击按钮的特定区域时才会发生这种情况。尽管它是不可见的,但实际的单选按钮的一小部分是可单击的(就在数字的右侧),当您单击它时,Bootstrap javascript 会阻止它更改其选中状态,但 javascript 会继续并添加事件类。我注意到这一点是因为当您将鼠标悬停在可单击的小单选按钮区域上时,鼠标手从指针变为默认光标。

虽然我想不出任何解决方案,但我的解决方案可能会产生负面影响。我不确定 Bootstrap 选择 opacity: 0; 而不是 visibility:hidden; 背后的原因,但也许更有知识的人可以提供一些。

更新

好吧,经过更多的挖掘,我发现了为什么他们使用不透明度而不是可见性。

In order to support the browser's form validation feedback, powered by the required attribute, we have to "hide" the inputs via opacity. We cannot use display: none; or visibility: hidden; as that also hides the popover. This way, we ensure a DOM element is visible to position the popover from.

See https://github.com/twbs/bootstrap/pull/12794 for more.

因此,如果您希望能够使用 native 浏览器验证,则几乎不能使用 visibility:hidden;

这实际上似乎是一个已知的错误,请参阅此 Github issue .

更新2

This solution可能更好:

[data-toggle="buttons"] > .btn > input[type="radio"],
[data-toggle="buttons"] > .btn > input[type="checkbox"] {
    width: 1px;
}

this solution可能会更好:

[data-toggle="buttons"] > .btn:after,
[data-toggle="buttons"] > .btn:after {
    content: "";
    display: block;
    width: 100%;
    height: 100%;
    position: absolute;
    left: 0;
    top: 0;
}

更新3

this可能是目前最好的解决方案。正如 Heinrich Fenkart here 所建议的那样.

[data-toggle="buttons"] > .btn > input[type="radio"],
[data-toggle="buttons"] > .btn > input[type="checkbox"] {
    clip: rect(1px, 1px, 1px, 1px);
    pointer-events: none;
}

而且看起来 Bootstrap 可能会在下一个版本中修复这个问题。

关于jquery - 使用 btn-group 时,单选按钮值未正确发送,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25447902/

相关文章:

javascript - 自动单击单选按钮但首先检查句子(标签)?

c# - 在 ASP.NET C# 上上传时如何验证文件大小?

javascript - WP中通过wrapper获取页面内容滑入

jquery - 使用 jquery 切换类

twitter-bootstrap - react Bootstrap 中的 Bootstrap "type alignment"

javascript - 使用键盘输入时,单选按钮的更改事件不会在 WebKit 中触发

php - jQuery UI 自动完成功能到 MySQL 与本地存储?

html - 如何将元素的字体设置为 Boostrap 的默认字体?

twitter-bootstrap - Bootstrap 表单布局 'correct' 方式?

php - 如何更改未选中的框错误。 (HTML)