繁体   English   中英

使用jQuery将项目从一个列表框移动到另一个列表框

[英]Moving items from one listbox to another using jquery

我基本上想使用Jquery将项目从一个列表框移动到另一个列表框。 我正在尝试在我的MVC应用程序中执行此操作。

下面注释的代码有效,但这不是我想要的。 因此,如果我在源列表框中选择一个项目,它将被添加到目标列表框中。 我基本上希望移动会在单击按钮时发生。 那就是我在下面写的$('#ShiftRight')。click(function()。在下面进一步,您将看到两个列表框和按钮代码。我面临的问题是当我单击按钮时,项目移动了到目的地一秒钟然后再次返回源头,有人可以告诉我为什么会这样吗?

//$('#sourceItems').change(function () {
//    $('#sourceItems option:selected').appendTo('#destinationItems');
//});

$('#ShiftRight').click(function () {
    $('#sourceItems option:selected').appendTo('#destinationItems');
});
<div class="Row">
    <div class="borderlessCell" style="vertical-align:top">
        <p>@Html.Label("List of Recipients")</p>
    </div>
    <div class="borderlessCell" style="vertical-align:top">
        @Html.ListBox("sourceItems", (IEnumerable<SelectListItem>)ViewBag.UserList, new { @style = "width: 250px;height: 130px;" })
    </div>
    <div class="borderlessCell" style="vertical-align:middle">
        <p><input type="submit" id="ShiftRight" value=">>" /> </p>
        <p> <input type="submit" id="ShiftLeft" value="<<" /></p>
    </div>

    <div class="borderlessCell">
        @Html.ListBox("destinationItems", new SelectList(new[] { "" }), new { @style = "width: 250px;height: 130px;" })
    </div>

它似乎重新出现在原始列表中的原因是因为您使用的是“提交”按钮,这会导致表单的回发。 input type="submit"更改为普通按钮:

<p><button id="ShiftRight">&gt;&gt;</button></p>
<p><button id="ShiftLeft">&lt;&lt;</button></p>

暂无
暂无

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

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