jQuery 如何在引导程序中获取单选按钮组值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18031608/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get radio button group values in bootstrap
提问by Talha
I am using following code of bootstrap for my radio buttons inside a form
我正在为表单内的单选按钮使用以下引导程序代码
<div class="btn-group" data-toggle="buttons-radio">
<button type="button" name="att" class="btn btn-primary active">Present</button>
<button type="button" name="att" class="btn btn-primary">Absent</button>
<button type="button" name="att" class="btn btn-primary">Leave</button>
</div>
You can find radio example by visiting http://getbootstrap.com/2.3.2/javascript.html#buttons
您可以通过访问http://getbootstrap.com/2.3.2/javascript.html#buttons找到无线电示例
I am using this for employees attendance, so there are multiple records of employees and there will be a array. I have tried question Get the value of a twitter bootstrap radio button. JQuerybut this returns me only last selected value.
我用这个来做员工考勤,所以有多个员工记录,会有一个数组。我试过问题Get the value of a twitter bootstrap radio button 。JQuery但这只返回我最后选择的值。
How can I solve this??
我该如何解决这个问题??
Note: I have also tried input tag instead of button but that effect styling
注意:我也尝试过输入标签而不是按钮,但效果样式
回答by Talha
I have solve the question by using
我已经通过使用解决了这个问题
<div id ="attend1" class="btn-group" data-toggle="buttons-radio">
<input type='hidden' name="attendy[]" value='default value'>
<button type="button" name="att" class="btn btn-primary active">Present</button>
<button type="button" name="att" class="btn btn-primary">Absent</button>
<button type="button" name="att" class="btn btn-primary">Leave</button>
</div>
and jquery
和jQuery
$(document).ready(function (){
$('.btn').click(function() {
var parent = '#' + $(this).parent().attr('id');
$(parent).find('input').val($(this).text());
});
});
Thanks alot everyone
非常感谢大家
回答by Mooseman
Add an id
attribute to .btn-group
and use this:
添加一个id
属性.btn-group
并使用它:
var activebtnvalue = $("#btngroup").find("button.active").prop('value');
Add a value
attribute to each button as well.
也value
为每个按钮添加一个属性。