如何在回发后使用ddSlick将所选内容设置为选项?

时间:2015-06-26 11:27:28

标签: javascript c# jquery asp.net drop-down-menu

我需要使用来自http://designwithpc.com/plugins/ddslick的图片下拉列表我试图在回发后设置“选定”选项,但我得到无限循环的回发。这是我的代码:

<form id="form1">
<select id="localeId" name="localeId"></select>
</form>

<script type="text/javascript">
        //Dropdown plugin data
        var ddData = [
            {
                text: "English",
                value: "en",
                selected: false,
                description: "English",
                imageSrc: "/assets/img/flags-icons/en-flag.png"
            },
            {
                text: "Portuguese",
                value: "pt",
                selected: false,
                description: "Portuguese",
                imageSrc: "/assets/img/flags-icons/pt-flag.png"
            },
            {
                text: "Russian",
                value: "ru",
                selected: false,
                description: "Russian",
                imageSrc: "/assets/img/flags-icons/ru-flag.png"
            },
            {
                text: "Spanish",
                value: "es",
                selected: false,
                description: "Spanish",
                imageSrc: "/assets/img/flags-icons/es-flag.png"
            }
        ];

        $('#localeId').ddslick({
            data: ddData,
            defaultSelectedIndex: 3,
            onSelected: function (data) {
                if (data.selectedIndex > 0) {
                    $('#hidCflag').val(data.selectedData.value);
                    $.cookie('lang', document.getElementById("hidCflag").value, { expires: 365 });
                    form1.submit();
                }

            }
        });
    </script>

请帮我解决一下?

2 个答案:

答案 0 :(得分:0)

您可以使用插件的选择方法,如

$('#demoSetSelected').ddslick('select', {index: i });

选择特定索引。

根据他们网站上的ddSlick演示#4(http://designwithpc.com/plugins/ddslick#demo

答案 1 :(得分:0)

通话:

    $( '#demoSetSelected' ).ddslick( 'select', { index: i } );

也会触发&#34; onSelected()&#34;您定义的函数导致无限循环。

我通过修改源文件(jquery.ddslick.js)并添加一个标志来禁用对onSelected()的调用来解决同样的问题:

  1. 将选择功能更改为:

    methods.select = function (options) {
        return this.each(function () {
            if (options.index)
                selectIndex($(this), options.index, options.disableTrigger);
        });
    }
    
  2. 修改selectIndex函数定义:

    function selectIndex(obj, index) {
    

    为:

    function selectIndex(obj, index, disableTrigger) {
    
  3. 在函数selectIndex(...)的最后,从:

    更改
    if (typeof settings.onSelected == 'function') {
        settings.onSelected.call(this, pluginData);
    }
    

    为:

    if ( !disableTrigger ) {
        if (typeof settings.onSelected == 'function') {
            settings.onSelected.call(this, pluginData);
        }
    }
    
  4. 然后改用:

        $( '#demoSetSelected' ).ddslick( 'select', { index: i, disableTrigger: true } );
    

    暂且不说:要按值而不是索引进行选择,请查看以下提及的代码:

    https://github.com/prashantchaudhary/ddslick/issues/78

    https://github.com/lunrfarsde/ddslick

    它是dd-slick的一个分支,删除了描述部分。但是按价值添加了选择。