gpt4 book ai didi

javascript - 创建依赖复选框单选按钮 - jQuery Mobile

转载 作者:行者123 更新时间:2023-11-29 15:29:45 25 4
gpt4 key购买 nike

我正在尝试在 jQuery Mobile 中创建几个依赖于限制复选框单选按钮组值的复选框单选按钮组。例如,如果选择了 6 的限制,我只想允许用户能够根据所有其他复选框单选按钮组选择的值选择最多 6 个 child ,并禁用其他所有内容。当限制发生变化时,我想相应地更新 UI。

每当单击任何复选框单选按钮时,我的更改事件处理程序中都有以下代码:

function updateUI(element) {
var limit = parseInt($('input[name="Limit_Total"]:checked').val(), 10);

// Children
var childCount = parseInt($('input[name="Child_Total"]:checked').val(), 10);
var secondChildCount = parseInt($('input[name="Second_Child_Total"]:checked').val(), 10);
var thirdChildCount = parseInt($('input[name="Third_Child_Total"]:checked').val(), 10);
var fourthChildCount = parseInt($('input[name="Fourth_Child_Total"]:checked').val(), 10);
var fifthChildCount = parseInt($('input[name="Fifth_Child_Total"]:checked').val(), 10);

// Totals
var totalChildern = childCount + secondChildCount + thirdChildCount + fourthChildCount + fifthChildCount;


// Enable the correct combination of children
$('input[name*="Child_Total"]').not(element).checkboxradio('disable').checkboxradio('refresh');
for (var i = 0; i <= 6; i++) {
if (i <= (limit - totalChildren)) {
$('input[id$="Child_Total_' + i + '"]').not(element).checkboxradio('enable').checkboxradio('refresh');
} else {
$('input[id$="Child_Total_' + i + '"]').not(element).attr('checked', false).checkboxradio('refresh');
}
}
}

我基本上想模拟下图中所示的行为:

enter image description here

问题是它并没有给我想要的行为。它会取消选择除我在组中选择的按钮以外的所有按钮。我试图找出最有效的方法来做到这一点,但我很难过。任何建议或帮助将不胜感激!

我设置了以下 jsfiddle 来演示 UI:http://jsfiddle.net/X8swt/29/

最佳答案

我设法用以下函数解决了我的问题:

$('div fieldset').each(function() { 
// Disable all none checked inputs
$(this).find('input:not(:checked)').checkboxradio().checkboxradio("disable").checkboxradio("refresh");
// Grab the selected input
var selectedElement = $(this).find('input:checked');
// Calculate the remaining children that can be selected
var remaining = (limit - totalChildern);
// Enable all inputs less than the selected input
$.each($(selectedElement).parent().prevAll().find('input'), function() {
$(this).checkboxradio().checkboxradio("enable").checkboxradio("refresh");
});
// Enable up to the remaining boxes past the selected input
$.each($(selectedElement).parent().nextAll().slice(0,remaining).find('input'), function() {
$(this).checkboxradio().checkboxradio("enable").checkboxradio("refresh");
});
});

请随时评论或批评我的解决方案。

关于javascript - 创建依赖复选框单选按钮 - jQuery Mobile,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35905900/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com