如何在回发后使用ddSlick将selected设置为选项

本文关键字:ddSlick selected 设置 选项 | 更新日期: 2023-09-27 17:59:48

我需要使用http://designwithpc.com/plugins/ddslick我试图在回发后设置"selected"选项,但我得到了无限的回发循环。这是我的代码:

<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>

请帮我解决它好吗?

如何在回发后使用ddSlick将selected设置为选项

调用:

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

还将触发您定义的"onSelected()"函数,导致无限循环。

我通过修改源文件(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. 在函数的最后选择Index(…),从:更改

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

    至:

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

然后改为使用:

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

顺便说一句:要按值而不是索引进行选择,请查看中提到的代码

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

https://github.com/lunrfarsde/ddslick

这是一个去掉了描述部分的分叉。但添加了按值选择。

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

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

以选择特定索引。

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