0

我正在完善我在 2 个下拉框上编写的一些 jquery。基本上,当第一个下拉列表更改时,它会过滤第二个下拉列表以仅显示适用的选项。如果在第一个下拉列表中选择了第一个选项(值:“none|”),则在第二个下拉列表中不显示任何选项

在大多数情况下,它工作得很好。这是问题所在:如果您在第一个下拉列表中选择第一个选项,则第二个下拉列表将被清除。但是,如果您随后在第一个下拉菜单中选择另一个选项,则第二个选项不应该时会保持空白。

如果您能帮我解决这个问题,我将不胜感激。您可以在此处找到下拉框:http: //goinspire.com/jwrp-hotel-registration/

PS:如果它有所作为(但我认为它没有),它是一个 WordPress 网站,表格是由 GravityForms 生成的。

PS:这是代码:

tripFilter = function () {

        var tripClass = '.hotel-trip select',
            hotelClass  = '.hotel-name select',
            unusedClass = '.unused-options select';
        jQuery(tripClass).change(function(){
            //testFunc();

            var tripSelect = jQuery(tripClass),
                trip = tripSelect.val(),
                hotelSelect = tripSelect.parents('form').find(hotelClass);
            if (trip === "none|"){
                jQuery(hotelClass+" > option").each(function() {
                    jQuery(this).clone().appendTo(unusedClass);
                    jQuery(this).remove();
                }); 
            } else {
                tripnum =  trip.match(/JWRP (\d*)/)[1];

                //var hotelChoices = [];
                jQuery(unusedClass+" > option").each(function() {
                        jQuery(this).clone().appendTo(hotelClass);
                        jQuery(this).remove();
                });
                jQuery(hotelClass+" > option").each(function() {
                    hotelMatch = jQuery(this).val().match(/(\d*) /);
                    //console.log(hotelMatch);
                    if (hotelMatch === null || hotelMatch[1] != tripnum){
                        jQuery(this).clone().appendTo(unusedClass);
                        jQuery(this).remove();
                        //console.log("not a match!");
                    };
                });

            }

    });
};




jQuery(document).ready(function () {
    tripFilter();
});
jQuery(document).bind('gform_post_render', function(event, form_id){
    if(form_id == 38) {
        tripFilter();
    }
});
4

1 回答 1

0

如果我理解正确,首先 dropdowm 引导第二。因此,我认为您可能只是在第一个下拉列表的“更改”事件上添加一个侦听器,以便在每次第一个输入的值发生某些更改时执行您的功能。

试试这个方法:

$(document).ready(function(){
    $('#38').bind('change', tripFilter)
});
于 2014-03-12T17:03:23.343 回答