获取twitter bootstrap单选按钮的值。 JQuery的 [英] Get the value of a twitter bootstrap radio button. JQuery

查看:183
本文介绍了获取twitter bootstrap单选按钮的值。 JQuery的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用twitter bootstrap中的单选按钮: http:// twitter。 github.com/bootstrap/javascript.html#buttons

I'm using the Radio-button from twitter bootstrap: http://twitter.github.com/bootstrap/javascript.html#buttons.

在我的html中看起来像这样:

In my html in looks like this:

  <div class="btn-group" data-toggle="buttons-checkbox">
  <button type="button" class="btn">Left</button>
  <button type="button" class="btn">Middle</button>
  <button type="button" class="btn">Right</button>
  </div>
  <input type="submit" onclick="get_the_value_and_do_something_with_it"

我想要知道如何通过Jquery或Javascript获取用户检查的按钮的值。

I would like to know how to get the value of the button the user checked thanks to Jquery or Javascript.

我在这个主题上看到了几个问题,但我认为这种情况有所不同按钮,因为看起来他们不接受已检查属性。

I saw several question on this topic, but I think it is different with this kind of button, because it looks like they do not accept a "checked" attribute.

非常欢迎任何帮助

PS:如果有人知道如何默认检查其中一个按钮,它也会对我有很大帮助。谢谢。

PS: if someone knows how to check by default one of the button, it would also help me a lot. Thank you.

推荐答案

因为标签显示它是按钮,那,你应该做它建议的,并使用这些按钮...这是一个简单的例子:

as the tag says it's a button, for that, you should do what it suggests, and work with those buttons... here's a simple example:

<input type="hidden" id="alignment" value="" />
<div class="btn-group alignment" data-toggle="buttons-checkbox">
    <button type="button" class="btn">Left</button>
    <button type="button" class="btn">Middle</button>
    <button type="button" class="btn">Right</button>
</div>

现在是jQuery:

$(".alignment .btn").click(function() {
    // whenever a button is clicked, set the hidden helper
    $("#alignment").val($(this).text());
}); 

提交后,您会在对齐隐藏字段。

upon submit, you will find the value in the alignment hidden field.

以下是一个基本示例: http:// jsbin .com / oveniw / 1 /

Here's a basic example: http://jsbin.com/oveniw/1/

要注意的一件好事是点击后,元素更改和引导程序将活动类名称附加到单击的按钮。

a good thing to take attention is that upon click, the element changes and bootstrap appends an active class name to the clicked button.

您始终可以使用它来更改隐藏字段,但如果我想自己提交表单,我会继续做我的例子。

You can always use this to change the hidden field, but I would continue to do my example if I wanted to submit the form it self.

这篇关于获取twitter bootstrap单选按钮的值。 JQuery的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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