php - 推特 Bootstrap : Get state of bootstrap checkbox buttons

标签 php button twitter-bootstrap

如何获取在 Bootstrap 中按下的按钮的“状态”?

即我正在使用这段代码,它使按钮像复选框一样“切换”:

<div class="btn-group" data-toggle="buttons-checkbox">
  <button type="button" class="btn btn-primary">Left</button>
  <button type="button" class="btn btn-primary">Middle</button>
  <button type="button" class="btn btn-primary">Right</button>
</div>

来自 the bootstrap manual here.

但是我随后单击提交按钮 - $_POST 中不包含任何数据。我怎么知道选择了哪个按钮?

我已经尝试将 'value="1"name="1"' 等添加到按钮 - 但仍然没有。

编辑:这是完整的解决方案,我可以使用下面 Felix 的帮助创建它。

<form method="post" action="" id="mform" class="form-horizontal">
 <div class="btn-group" data-toggle="buttons-checkbox">
   <button type="button" name="left"   value="1" class="btn btn-primary">Left</button>
   <button type="button" name="middle" value="1" class="btn btn-primary">Middle</button>
   <button type="button" name="right"  value="1" class="btn btn-primary">Right</button>
 </div>
 <button type="submit" class="btn">Submit</button>
</form>

<script>
    $('#mform').submit(function() {
        $('#mform .btn.active').each(function() {
             var input = document.createElement("input");
             input.setAttribute("type", "hidden");
             input.setAttribute("name", this.name);
             input.setAttribute("value", this.value);
             document.getElementById("mform").appendChild(input);
         });
    });
 </script>

最佳答案

“选中”的按钮将附加 active 类。然后,您可以使用 jQuery 仅选择这些按钮。由于 button 是表单元素,它们可以具有 namevalue 属性,最好利用这些属性。

例子:

var data = {};

$('#myButtonGroup .btn.active').each(function() {
    data[this.name] = this.value;
});

要将数据发送到服务器,您可以使用 Ajax 并手动设置数据/将其与其他表单数据(如果存在)合并。

如果您想使用“传统”表单提交,您也可以尝试将值镜像到提交时的隐藏表单元素中(参见 Change form values after submit button pressed)。

关于php - 推特 Bootstrap : Get state of bootstrap checkbox buttons,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14035640/

相关文章:

php - 无法读取 GROUP_CONCAT 结果中的特定值

android - 带 fragment 的按钮

android - 按钮点击只工作一次

css - 在文件输入中隐藏浏览按钮

html - Bootstrap 固定导航栏 - 自定义 css

html - 悬停时的 Bootstrap 下拉自定义

jquery - Bootstrap col-xs 不工作

php - Debian Stable php-5.4 上的 Symfony2 JsonResponse utf8 编码问题

php - 如果冒号是第一个字符,则删除它

php - 使用基于文件的 CMS 将开发/生产与 git 同步