Javascript 创建依赖的chechbox单选按钮-jQuery Mobile
我试图在jQuery mobile中创建几个依赖于限制checkboxradio按钮组值的checkboxradio按钮组。例如,如果选择了限制6,我只希望允许用户能够基于所有其他复选框单选按钮组选定值选择总共6个子项,并禁用所有其他项。当限制改变时,我想相应地更新UI 每当单击任何复选框单选按钮时,我的更改事件处理程序中都有以下代码:Javascript 创建依赖的chechbox单选按钮-jQuery Mobile,javascript,jquery,jquery-mobile,Javascript,Jquery,Jquery Mobile,我试图在jQuery mobile中创建几个依赖于限制checkboxradio按钮组值的checkboxradio按钮组。例如,如果选择了限制6,我只希望允许用户能够基于所有其他复选框单选按钮组选定值选择总共6个子项,并禁用所有其他项。当限制改变时,我想相应地更新UI 每当单击任何复选框单选按钮时,我的更改事件处理程序中都有以下代码: function updateUI(element) { var limit = parseInt($('input[name="Limit_Total
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');
}
}
}
函数更新(元素){
var limit=parseInt($('input[name=“limit\u Total”]:checked').val(),10);
//孩子们
var childCount=parseInt($('input[name=“Child_Total”]:checked').val(),10);
var secondChildCount=parseInt($('input[name=“Second\u Child\u Total”]:checked').val(),10);
var thirdChildCount=parseInt($('input[name=“Third\u Child\u Total”]:checked').val(),10);
var fourthChildCount=parseInt($('input[name=“Fourth\u Child\u Total”]:checked').val(),10);
var fifthChildCount=parseInt($('input[name=“Fifth\u Child\u Total”]:checked').val(),10);
//总数
var totalChildern=childCount+secondChildCount+thirdChildCount+fourthChildCount+fifthChildCount;
//启用子项的正确组合
$('input[name*=“Child_Total”]')。非(元素)。checkboxradio('disable')。checkboxradio('refresh');
对于(var i=0;i我设法用以下函数解决了我的问题:
$('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");
});
});
请随意评论或评论我的解决方案。做得好。您不需要。checkboxradio()
它已经增强了。