如何动态过滤jQuery移动选择表单

How to filter jQuery Mobile select form dynamically

本文关键字:jQuery 移动 选择 表单 过滤 何动态 动态      更新时间:2023-09-26

我试图通过使用另一个选择列表来过滤选择列表。问题是,我不能重新加载列表后,它被过滤。因此,如果用户在第一个列表中不小心做出了错误的选择,那么第二个列表中就不会留下任何选项。你可以在下面看到我的代码,谢谢。

        $('#selectCftDialog').change(function() {
            //alert('Handler for .select() called.');
            var tempCftID;
            tempCftID = $("#selectCftDialog").val();
            //alert("The tempCraftID is: CftID-" + tempCraftID)
            $('#selectSpDialog option').not('#CftID-' + tempCftID).remove();    
                    });

你可以做的是在select元素本身启动时将选项保存为data

$('#selectSpDialog').data('options', $('#selectSpDialog option')); // Set the jquery data `options` with the initial set of options.
$('#selectCftDialog').change(function () {
    //alert('Handler for .select() called.');
    var tempCftID;
    tempCftID = $("#selectCftDialog").val();
    //alert("The tempCraftID is: CftID-" + tempCraftID)
    $('#selectSpDialog option').not('#CftID-' + tempCftID).remove(); // Ok you can remove now
});
function someEventCalledProabablyReset()
{
    var select = $('#selectSpDialog');
    select.html(select.data('options')); //Set back all the saved options.
}

小提琴