[英]How to swap 2 @Html.DropDownListFor with Jquery/JS
I found some examples in SO.我在 SO 中找到了一些例子。 But none of them worked for me.但他们都没有为我工作。 I want to swap values of 2 @Html.DropDownListFor
each other.我想交换 2 个@Html.DropDownListFor
值。 Both DropDownList contains the same set of values except the default value to show.除了要显示的默认值之外,两个 DropDownList 都包含相同的一组值。 Below is my code.下面是我的代码。
<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="▲" 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>
The start
and end
variables are not correctly set. start
和end
变量设置不正确。 The selectors could be something like the following.选择器可能类似于以下内容。
var start = row.find(".swap_dropdown select:first");
var end = row.find(".swap_dropdown select:last");
The rest of the code worked, see js-fiddle here .其余代码有效,请参阅 js-fiddle here 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.