问题描述
我的HTML部分如下:
My HTML part is below:
<input id="yes" save-value="yes" value="no" name="view_y" class="switch-input yes" type="radio">
<label checked="checked" class="switch-label switch-label-off selected" for="yes">Yes</label>
<input id="no" save-value="no" value="no" name="view_n" class="switch-input no" type="radio">
<label class="switch-label switch-label-on" for="no">No</label>
<span class="switch-selection"></span>
当我单击每个按钮的"checked ="checked"时,所选选项将从"yes"变为"no"标签.
When I click each button checked="checked" and selected options will be changing from yes to no labels.
使用这些选项,如何获得所选单选按钮的保存值.因为,我在我的value选项上使用了Handlebars js(json),所以我想获取所选单选按钮的保存值.
With these options, how can I get the save-value of selected radio button. Since, am using Handlebars js (json) on my value option I want to get the save-value of selected radio button.
我该怎么做?
推荐答案
假定类switch-input
仅分配给这两个输入元素,则可以将其与选中的选择器一起使用
Assuming the class switch-input
is assigned to only these two input elements, you can use it along with the checked selector
var value = $('input.switch-input').filter(':checked').attr('save-value');
//or var value = $('input.switch-input:checked').attr('save-value');
注意:更喜欢使用data- *属性来存储自定义属性
Note: Prefer to use data-* attributes to store custom attributes
我认为单选按钮实际上并没有得到检查,只是标签属性被更改,因此,向myclass
I think the radio buttons are not actually getting checked, only the labels attributes are changed so, add a additional class to the labels like myclass
<input id="yes" save-value="yes" value="no" name="view_y" class="switch-input yes" type="radio"/>
<label checked="checked" class="myclass switch-label switch-label-off selected" for="yes">Yes</label>
<input id="no" save-value="no" value="no" name="view_n" class="switch-input no" type="radio"/>
<label class="myclass switch-label switch-label-on" for="no">No</label>
<span class="switch-selection"></span>
然后
var value = $('.myclass[checked="checked"]').prev('input').attr('save-value');
console.log(value)
这篇关于如何获取所选单选按钮的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!