0

我正在使用 ASP MVC 3,当我更改 dropdownlistfor 的值时,目前在文本框中获取新数据时遇到问题。

<div id="BagelProductEdit">
            <h2>Bagel</h2>
             @Html.DropDownListFor(x => x.SelectedOptionBagel, Model.LstBagelsList, new { @class = "width200", @onchange = "refreshTextBoxFors()" })   
             <br /> 

               <table>
                <tr>
                    <td>Bagelnaam</td><td> @Html.TextBoxFor(x => x.LstBagels.ElementAt(x.SelectedOptionBagel).Name,new { Name ="txtEditBagelName" })</td>
                </tr>
                <tr>
                    <td>Vertaling</td><td> @Html.TextBoxFor(x => x.LstBagels.ElementAt(x.SelectedOptionBagel).NameFr,new { Name ="txtEditBagelNameFr" })</td>
                </tr>
                <tr>
                    <td>Prijs</td>
                    <td> @Html.TextBoxFor(x => x.LstBagels.ElementAt(x.SelectedOptionBagel).Price,new { Name ="txtEditBagelPrice" })</td>
                </tr>   

            </table>
             <input class="button widthorderProduct" type="submit" name="BtnEditBagel" value="Edit een bagel" />
        </div>

当我的下拉列表的值发生变化时,如何在我的文本框中获取刷新的值?

4

1 回答 1

0

您可以向该下拉列表添加一个 ID:

@Html.DropDownListFor(x => x.SelectedOptionBagel, Model.LstBagelsList, new { @class = "width200", @onchange = "refreshTextBoxFors()", id= "myDDL" })  

然后,refreshTextBoxFors()您可以通过这种方式访问​​下拉列表中的选定值(假设您使用的是jQuery):

$("#myDDL").val();

如果您需要选定的文本(不是值):

$("#myDDL :selected").text();

如果您没有使用 jQuery,而是使用普通Javascript,您可以使用它来获取 DropDownList 中的选定值:

var e = document.getElementById("myDDL");
var selValue = e.options[e.selectedIndex].value;

编辑 - 设置文本框的数据:

选择 DDL 值后,您可以执行以下操作:

if(selValue == '1'){
   $('#test').val('here is the value you want to put');
}
else{
$('#test').val('other value');
}

现在,在您评论中的示例中,您似乎有一个 TextBoxFors 列表。因此,如果您将 id 放入循环中,它会将其设置为您的所有文本框。您可以放置​​ a @class = "test"instead 并使用$('.test')instead of访问文本框$('#test')

于 2013-10-29T18:13:04.107 回答