简体   繁体   English

多个列表框的jQuery代码有什么问题?

[英]What's wrong with this jQuery code for multiple listboxes?

I can't get the following jQuery code to work (it doesn't transfer selected items between listboxes) in an MVC 5 app: 我无法在MVC 5应用中使以下jQuery代码正常工作(它不会在列表框之间传输所选项目):

<script>
        $(function () {
            $("add").click(function () {
                $("#listBoxAvail > option:selected").each(function () {
                    $(this).remove().appendTo("#listBoxSel");
                });
            });

            $("remove").click(function () {
                $("#listBoxSel > option:selected").each(function () {
                    $(this).remove().appendTo("#listBoxAvail");
                });
            });
        });

    </script>

The rest of the markup with the listboxes is: 带有列表框的标记的其余部分是:

@using (Html.BeginForm())
        {
           @Html.ListBoxFor(m => m.SelectedAttributes, Model.Attributes, new {id="listBoxAvail", SIZE = 5} ) 

            <input type="submit" name="add" 
                   id="add" value="MoveRight" />

            <input type="submit" name="remove" 
                   id="remove" value="MoveLeft" />

            <input type="submit" name="remove-all" id="remove-all" value="RemAll" />

            <input type="submit" name="select-all" id="select-all" value="SelAll" />

            @Html.ListBoxFor(m => m.SelectedAttributes2, Model.SelectedItems, new { id = "listBoxSel", SIZE = 5})
        } 
    </div>

The ViewModel is: ViewModel是:

public class OptInViewModel
    {
        public IEnumerable<string> SelectedAttributes { get; set; }
        public IEnumerable<string> SelectedAttributes2 { get; set; }
        public IEnumerable<SelectListItem> Attributes { get; set; }
        public IEnumerable<SelectListItem> SelectedItems { get; set; }
    }

And the controller is: 控制器是:

 public ActionResult Index()
        {
            AttributeEntities db = new AttributeEntities();
            List<SelectListItem> listSelectListItems = new List<SelectListItem>();
            List<SelectListItem> listSelItems = new List<SelectListItem>();

            foreach (var attributes in db.HarmonyAttributes)
            {
                SelectListItem selectList = new SelectListItem
                {
                    Text = attributes.AttributeName,
                    Value = attributes.AtrributeLabel,
                    Selected = false
                };
                listSelectListItems.Add(selectList);
            }

            foreach (var sel in db.SelectedHarmonyAttributes)
            {
                SelectListItem selList = new SelectListItem
                {
                    Text = sel.CustomLabel,
                    Value = sel.HarmonyAttribute_ID.ToString(),
                    Selected = false
                };
                listSelectListItems.Add(selList);
            }

            OptInViewModel viewModel = new OptInViewModel
            {
                Attributes = listSelectListItems,
                SelectedItems = listSelItems
            };
        return View(viewModel);
    }
}

I can't figure out why the JQuery script won't work. 我不知道为什么JQuery脚本不起作用。 What am I doing wrong? 我究竟做错了什么?

Looks like selector is wrong, you are trying to assign handler to element "add": 看起来选择器是错误的,您正在尝试将处理程序分配给元素“ add”:

$("add")

instead of element with id "add" 而不是ID为“ add”的元素

$("#add")

Same for "remove" element. 与“删除”元素相同。

I know I am years late, but it might help somebody else. 我知道我晚了几年,但可能会对其他人有所帮助。

It's refreshing because you buttons are o type submit. 令人耳目一新,因为您的按钮是o提交类型。

Change this : 改变这个:

<input type="submit" name="remove" 
               id="remove" value="MoveLeft" />

To this : 对此:

<input type="button" name="remove" 
               id="remove" value="MoveLeft" />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM