我在书中找到了一些例子。但他们都不适合我。我想互相交换2@Html.DropDownListFor的值。除了要显示的默认值外,两个DropDownList都包含相同的值集。下面是我的代码。

<div class="currency_swap">
<p>
    <label>From</label>
    <div class="swap_dropdown">
        @Html.DropDownListFor(m => m.SourceCurrencyname, new SelectList(ViewBag.CurrencyNames, "Currency_Code", "Currency_Code"), "---- All ----")
    </div>
</p>
<div><input type="button" id="swap_up" value="&#x25B2" class="swap" /></div>
<p>
    <label>To</label>
    <div class="swap_dropdown">@Html.DropDownListFor(m => m.TargetCurrencyname, new SelectList(ViewBag.CurrencyNames, "Currency_Code", "Currency_Code"), "Agency Currency")</div>
</p>
</div>

<script type="text/javascript">
$(".swap").click(function () {
    var row = $(this).closest(".currency_swap");
    var start = row.find("swap_dropdown:first select");
    var end = row.find("swap_dropdown:last select");
    var temp = start.val();
    start.val(end.val());
    end.val(temp);
});
</script>

最佳答案

startend变量设置不正确。选择器可能如下所示。

var start = row.find(".swap_dropdown select:first");
var end = row.find(".swap_dropdown select:last");

剩下的代码工作,请参见js fiddlehere

关于javascript - 如何使用Jquery/JS交换2 @ Html.DropDownListFor,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/31247114/

10-13 01:42