Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/86.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
jquery根据另一个选择输入更改选择输入选择值_Jquery - Fatal编程技术网

jquery根据另一个选择输入更改选择输入选择值

jquery根据另一个选择输入更改选择输入选择值,jquery,Jquery,我有两个选择输入,一个用于客户,另一个用于票证,具有以下选项结构: 顾客 如果我试过的话: $("#ticket").not("[customer*=" + $(this).val() + "]").hide(); 及 但这两种方法都没有达到预期效果。更新:更改选项以反映select2下拉列表中的新信息 试试这个:请参阅工作演示 <select id="customer"> </select> <select id="ticket"> </selec

我有两个选择输入,一个用于客户,另一个用于票证,具有以下选项结构:

顾客 如果我试过的话:

$("#ticket").not("[customer*=" + $(this).val() + "]").hide();

但这两种方法都没有达到预期效果。

更新:更改选项以反映select2下拉列表中的新信息

试试这个:请参阅工作演示

<select id="customer">
</select>

<select id="ticket">
</select>

<script type="text/javascript">
    $(document).ready(function() {
        // data array all 'customer' options
        var customers = [
            { id: '0', text: '- Customers -'},
            { id: '123', text: 'customer 123'},
            { id: '124', text: 'customer 124'},
            { id: '125', text: 'customer 125'},
            { id: '126', text: 'customer 126'},
            { id: '127', text: 'customer 127'},
        ];
        // populate customer dropdown with options from array
        $("#customer").select2({
            data: customers
        }); 
        // data array for all 'ticket' options
        var tickets = [
            { id: '0', text: '- Tickets -', customer: ''},
            { id: '1', text: 'ticket 1', customer: '123'},
            { id: '2', text: 'ticket 2', customer: '123'},
            { id: '3', text: 'ticket 3', customer: '124'},
            { id: '4', text: 'ticket 4', customer: '126'},
            { id: '5', text: 'ticket 5', customer: '126'},
        ];  

        // function to populate tickets both on load and on change of 'customer'
        jQuery.fn.setTickets = function(reset) {
            // if flagged to reset, this will empty the select2() options for 'ticket' below
            if (! reset) { var reset = false; }

            // get currently-selected customer value
            var customer = $(this).val();

            // build new data array using 'tickets' as our data source, but then excluding
            // options that don't match the 'customer' value above
            var new_tickets = [];
            for (var i=0; i < tickets.length; i++) {
                // includes ticket if no customer is selected ('0') or if the ticket itself has
                // no id/value (id '0' = '- Tickets -', or our blank title option), or if the
                // customer matches the 'customer' attribute of the tickets data array
                if (customer=='0' || tickets[i].id=='0' || customer==tickets[i].customer) {
                    new_tickets.push(tickets[i]);
                }
            }

            // reset previously-populated select2() options
            if (reset) {
                $("#ticket").select2('destroy').empty();
            }
            // populate tickets with new 'new_tickets' data array, and trigger the change to 
            // tell select2() to re-build options
            $("#ticket").select2({ data: new_tickets }).trigger('change');
        }
        // on document load, build tickets with no reset
        $("#customer").setTickets();

        // on customer change, re-populate tickets with reset passed in to erase previous ticket options
        $("#customer").change(function(){
            $(this).setTickets(1);
        });
    });
</script>

companyid来自何处?很抱歉,我在指向客户下拉列表时使用了错误的引用名称,我刚刚更新了代码。我想是这样:-它可以工作,但当我更改所选客户选项时,它不会隐藏其他票证选项它对我有效。票证下拉列表将默认选择-tickets-选项,并将隐藏不相关的选项。您使用的是哪个jquery版本?我不认为这会有什么不同,但我正在使用select2I更新下面的答案,以反映您的select2场景。如果它适合你,你能把它标记为已接受吗?
$("#customer").change(function(){
    if($(this).val() !== $("#ticket").find('option:selected').attr('customer')) {

    }
});
$("#ticket").not("[customer*=" + $(this).val() + "]").hide();
$("#ticket option[customer=" + $(this).val() + "]").hide();
<select id="customer">
</select>

<select id="ticket">
</select>

<script type="text/javascript">
    $(document).ready(function() {
        // data array all 'customer' options
        var customers = [
            { id: '0', text: '- Customers -'},
            { id: '123', text: 'customer 123'},
            { id: '124', text: 'customer 124'},
            { id: '125', text: 'customer 125'},
            { id: '126', text: 'customer 126'},
            { id: '127', text: 'customer 127'},
        ];
        // populate customer dropdown with options from array
        $("#customer").select2({
            data: customers
        }); 
        // data array for all 'ticket' options
        var tickets = [
            { id: '0', text: '- Tickets -', customer: ''},
            { id: '1', text: 'ticket 1', customer: '123'},
            { id: '2', text: 'ticket 2', customer: '123'},
            { id: '3', text: 'ticket 3', customer: '124'},
            { id: '4', text: 'ticket 4', customer: '126'},
            { id: '5', text: 'ticket 5', customer: '126'},
        ];  

        // function to populate tickets both on load and on change of 'customer'
        jQuery.fn.setTickets = function(reset) {
            // if flagged to reset, this will empty the select2() options for 'ticket' below
            if (! reset) { var reset = false; }

            // get currently-selected customer value
            var customer = $(this).val();

            // build new data array using 'tickets' as our data source, but then excluding
            // options that don't match the 'customer' value above
            var new_tickets = [];
            for (var i=0; i < tickets.length; i++) {
                // includes ticket if no customer is selected ('0') or if the ticket itself has
                // no id/value (id '0' = '- Tickets -', or our blank title option), or if the
                // customer matches the 'customer' attribute of the tickets data array
                if (customer=='0' || tickets[i].id=='0' || customer==tickets[i].customer) {
                    new_tickets.push(tickets[i]);
                }
            }

            // reset previously-populated select2() options
            if (reset) {
                $("#ticket").select2('destroy').empty();
            }
            // populate tickets with new 'new_tickets' data array, and trigger the change to 
            // tell select2() to re-build options
            $("#ticket").select2({ data: new_tickets }).trigger('change');
        }
        // on document load, build tickets with no reset
        $("#customer").setTickets();

        // on customer change, re-populate tickets with reset passed in to erase previous ticket options
        $("#customer").change(function(){
            $(this).setTickets(1);
        });
    });
</script>