如何统一从多组单选按钮和多组复选框中获取的文本? [英] How can I unify text that take from multiple group radio button and multiple group checkbox?
问题描述
我的javascript代码如下:
My javascript code like this :
$(function(){
$('input[type="radio"]').click(function(){
var $radio = $(this);
var name = $(this).prop("name");
// if this was previously checked
if ($radio.data('waschecked') == true)
{
$radio.prop('checked', false);
$radio.data('waschecked', false);
$('#result-select').text('');
}
else{
$radio.data('waschecked', true);
$("input[name=\""+name+"\"]:not(:checked)").data('waschecked', false);
$('#result-select').text(txt);
}
var output = [];
var txt;
$('input[type="radio"]:checked').each( function() {
txt = $(this).parent().text();
output.push(txt);
});
$('#result-select').text(output.join(' - '));
});
});
演示和完整代码如下: https://jsfiddle.net/oscar11/m7by6zcw/41/
Demo and full code like this : https://jsfiddle.net/oscar11/m7by6zcw/41/
我想:
如果我选择chelsea,madrid和juve,结果如下:
If I select chelsea, madrid and juve the result like this :
切尔西 - 马德里 - 尤文
Chelsea - Madrid - Juve
如果我选择chelsea和madrid结果如下:
If I select chelsea and madrid the result like this :
Chelsea - Madrid
Chelsea - Madrid
如果我选择chelsea,madrid,juve和milan,结果就像这个:
If I select chelsea, madrid, juve and milan the result like this :
切尔西 - 马德里 - 尤文 - 米兰
Chelsea - Madrid - Juve - Milan
因此,如果我选中单选按钮,它会显示文本。如果取消选中单选按钮,则不显示文本。我检查组合框,它显示文本。如果我取消选中组合框,则不显示文本
So if I check radio button, it display the text. If I uncheck radio button, it not display the text. I check combobox, it display the text. If I uncheck combobox, it not display the text
例如文字:
<切尔西 - 马德里 - 尤文 - 米兰
Chelsea - Madrid - Juve - Milan
我取消选中尤文,结果如下:
I uncheck Juve, the result like this :
切尔西 - 马德里 - 米兰
Chelsea - Madrid - Milan
在单选按钮上,它有效
但是在复选框上,我仍然感到困惑
On the radio button, it works But on the checkbox, I'm still confused
我该怎么办?
更新:
单选按钮可以取消选中
例如:
如果我选择chelsea,madrid和juve,结果如下:
If I select chelsea, madrid and juve the result like this :
切尔西 - 马德里 - Juve
Chelsea - Madrid - Juve
然后我取消选中madrid,结果如下:
Then I uncheck madrid, the result like this :
Chelsea - Juve
Chelsea - Juve
推荐答案
您需要修改输入元素选择器处理两种输入类型检查盒子和单选按钮:
You need to modify the input element selector to handle both input types checkboxes and radio buttons:
$('.list-unstyled input').....
Working Demo
这篇关于如何统一从多组单选按钮和多组复选框中获取的文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!