Why the jQuery Selectable plugin doesn't work with a foreach generated list?

旧时模样 提交于 2019-12-24 06:59:20

问题


I am currently working on an MVC project and implemented jQuery Selectable plugin. I have a string in my model which I have Split() as below in my view:

@{
    var size = Model.AvailableSizes.Split(',');
    foreach (var item in size)
    {
        <ol class="ui-selectable" id="selectable">
            <li class="ui-selectable">@item</li>
        </ol>
    }
} 

Here is the static script defined in my view:

<script type="text/javascript">
    $(document).ready(function () {
        $("#selectable").selectable();
    });
</script>

The selectable jQuery plugin successfully generates the <ol>s for each item but I can only select the first item not the rest. What is the problem?


回答1:


You should put the <ol> outside the loop. Right now you're creating a N ol elements with a single li inside them, all with the same id which is both invalid HTML, and the cause of your problem.

Try this:

<ol class="ui-selectable" id="selectable">
    @{
        var size = Model.AvailableSizes.Split(',');
        foreach (var item in size)
        {
            <li class="ui-selectable">@item</li>
        }
    } 
</ol>


来源:https://stackoverflow.com/questions/39043635/why-the-jquery-selectable-plugin-doesnt-work-with-a-foreach-generated-list

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!