Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/71.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_Datepicker_Associative Array - Fatal编程技术网

Jquery 如何在关联数组中获取键和值

Jquery 如何在关联数组中获取键和值,jquery,datepicker,associative-array,Jquery,Datepicker,Associative Array,我有一个datepicker,它只基于3组数组启用了一些日期。 在选择时,我检索单击的可用日期。 现在事情变得复杂了。具有可用日期的数组需要是id和日期为“key”和“value”的关联数组。如何检索与我单击的可用日期关联的id 以下是我目前的代码: function calendar(){ function datePicker(array){ $j("#datepicker").datepicker({ dateFormat: 'dd-mm-

我有一个datepicker,它只基于3组数组启用了一些日期。 在选择时,我检索单击的可用日期。 现在事情变得复杂了。具有可用日期的数组需要是id和日期为“key”和“value”的关联数组。如何检索与我单击的可用日期关联的id

以下是我目前的代码:

function calendar(){

    function datePicker(array){
        $j("#datepicker").datepicker({
            dateFormat: 'dd-mm-yy',
            minDate: $j.datepicker.parseDate('ddmmyy', array[0]), //this makes the datepicker start at the first available
            beforeShowDay: function(dateToShow){
                if ($j.inArray($j.datepicker.formatDate('ddmmyy', dateToShow),array) !== -1) return [1, 'my-class', 'Available date!'];
                else return [0, 'no-class', 'Date not available!'];

            },
            //get the selected date
            onSelect: function(dateText) {
                var dateAsString = dateText; //the first parameter of this function

                //var dateAsObject = $j(this).datepicker( 'getDate' ); //the getDate method
                $j('#getDate').text(dateAsString);
            }
        });
    }


    $j('.book-pack a.button').each(function(){

        //getting the button id
        var btnId = $j(this).attr('id');

        //arrays with available dates for the 3 packs
        var datesClub = new Array('08102010', '09102010', '15102010', '16102010', '22102010', '23102010', '29102010', '30102010');
        var datesEssential = new Array('08102010', '09102010', '15102010', '16102010', '22102010', '23102010', '29102010', '30102010');
        var datesFling = new Array('08102010', '09102010', '15102010', '16102010', '22102010', '23102010', '29102010', '30102010');


        $j(this).click(function(){

            switch(btnId)
            {
                case 'club-class':
                    $j('.window h2.page-title').text('Club Class');
                    datePicker(datesClub);
                    break;
                case 'essential':
                    $j('.window h2.page-title').text('The Essential Experience');
                    datePicker(datesEssential);
                    break;
                case 'last-fling':
                    $j('.window h2.page-title').text('Last Fling Before the Ring');
                    datePicker(datesFling);
                    break;
            }

            return false;
        });
    });
}
试试这样的方法():

此脚本从关联数组(对象)创建日期数组。切换key:value对比较容易,因此日期是键,值是id,但我认为这会让人理解这个想法

function calendar() {

    function datePicker(pack) {
        // create array of dates from pack
        var i, array = [];
        for (var i in pack) {
            array.push(i);
        }
        $j("#datepicker").datepicker({
            dateFormat: 'dd-mm-yy',
            minDate: $j.datepicker.parseDate('ddmmyy', array[0]),
            //this makes the datepicker start at the first available
            beforeShowDay: function(dateToShow) {
                return ($j.inArray($j.datepicker.formatDate('ddmmyy', dateToShow), array) !== -1) ? [1, 'my-class', 'Available date!'] : [0, 'no-class', 'Date not available!'];
            },
            //get the selected date
            onSelect: function(dateText, inst) {
                var dateAsString = dateText,
                    //the first parameter of this function
                    dateCompressed = (dateText).replace(/-/g, ''); // dd-mm-yyyy -> ddmmyyyy
                $j('#getDate').text(dateAsString);
                $j('#getId').text(pack[dateCompressed]);
            }

        });
    }


    $j('.book-pack a.button').each(function() {
        //getting the button id
        var btnId = $j(this).attr('id');
        //arrays with available dates for the 3 packs
        var datesClub = { '08102010' : 'id1', '09102010' : 'id2', '15102010' : 'id3', '16102010' : 'id4', '22102010' : 'id5', '23102010' : 'id6', '29102010' : 'id7', '30102010' : 'id8' };
        var datesEssential =  { '08102010' : 'id1', '09102010' : 'id2', '15102010' : 'id3', '16102010' : 'id4', '22102010' : 'id5', '23102010' : 'id6', '29102010' : 'id7', '30102010' : 'id8' };
        var datesFling =  { '08102010' : 'id1', '09102010' : 'id2', '15102010' : 'id3', '16102010' : 'id4', '22102010' : 'id5', '23102010' : 'id6', '29102010' : 'id7', '30102010' : 'id8' };

        $j(this).click(function() {
            switch (btnId) {
            case 'club-class':
                $j('.window h2.page-title').text('Club Class');
                datePicker(datesClub);
                break;
            case 'essential':
                $j('.window h2.page-title').text('The Essential Experience');
                datePicker(datesEssential);
                break;
            case 'last-fling':
                $j('.window h2.page-title').text('Last Fling Before the Ring');
                datePicker(datesFling);
                break;
            }
            return false;
        });
    });
}
试试这样的方法():

此脚本从关联数组(对象)创建日期数组。切换key:value对比较容易,因此日期是键,值是id,但我认为这会让人理解这个想法

function calendar() {

    function datePicker(pack) {
        // create array of dates from pack
        var i, array = [];
        for (var i in pack) {
            array.push(i);
        }
        $j("#datepicker").datepicker({
            dateFormat: 'dd-mm-yy',
            minDate: $j.datepicker.parseDate('ddmmyy', array[0]),
            //this makes the datepicker start at the first available
            beforeShowDay: function(dateToShow) {
                return ($j.inArray($j.datepicker.formatDate('ddmmyy', dateToShow), array) !== -1) ? [1, 'my-class', 'Available date!'] : [0, 'no-class', 'Date not available!'];
            },
            //get the selected date
            onSelect: function(dateText, inst) {
                var dateAsString = dateText,
                    //the first parameter of this function
                    dateCompressed = (dateText).replace(/-/g, ''); // dd-mm-yyyy -> ddmmyyyy
                $j('#getDate').text(dateAsString);
                $j('#getId').text(pack[dateCompressed]);
            }

        });
    }


    $j('.book-pack a.button').each(function() {
        //getting the button id
        var btnId = $j(this).attr('id');
        //arrays with available dates for the 3 packs
        var datesClub = { '08102010' : 'id1', '09102010' : 'id2', '15102010' : 'id3', '16102010' : 'id4', '22102010' : 'id5', '23102010' : 'id6', '29102010' : 'id7', '30102010' : 'id8' };
        var datesEssential =  { '08102010' : 'id1', '09102010' : 'id2', '15102010' : 'id3', '16102010' : 'id4', '22102010' : 'id5', '23102010' : 'id6', '29102010' : 'id7', '30102010' : 'id8' };
        var datesFling =  { '08102010' : 'id1', '09102010' : 'id2', '15102010' : 'id3', '16102010' : 'id4', '22102010' : 'id5', '23102010' : 'id6', '29102010' : 'id7', '30102010' : 'id8' };

        $j(this).click(function() {
            switch (btnId) {
            case 'club-class':
                $j('.window h2.page-title').text('Club Class');
                datePicker(datesClub);
                break;
            case 'essential':
                $j('.window h2.page-title').text('The Essential Experience');
                datePicker(datesEssential);
                break;
            case 'last-fling':
                $j('.window h2.page-title').text('Last Fling Before the Ring');
                datePicker(datesFling);
                break;
            }
            return false;
        });
    });
}