javascript - 如何使用javascript获取下拉列表的选定值

标签 javascript asp.net-mvc html.dropdownlistfor

我有一个表,其中有几个@Html.dropdowlistfor

我试图使用javascript读取所选值,但所有读取的都是生成的html。 我怎样才能读到它?

for (var i = 0; i < oTable.length; i++) {
  **userModel.Id    = oTable[i][0];**

  regionModel.Users.push(userModel);
  processModel.Regions.push(regionModel);

  userModel   = { "Id": "", "Name": ""};
  regionModel = { "Id": "", "Name": "", "Users": []};
}

表格

<table class="tbl" id="tbl">
        <thead>
            <tr>
                <th>
                    Region
                </th>
                <th>
                    Owner
                </th>
            </tr>
        </thead>
        <tbody>
            @if (Model != null)
            {
                foreach (var item in Model.Regions)
                {
                <tr>
                    <td>
                        @Html.DisplayTextFor(i => item.Name)
                    </td>
                    <td>
                        @Html.DropDownListFor(i => item.Users, new SelectList(item.Users, "Id", "Name"))
                    </td>
                </tr>
            }
            }
        </tbody>

代码

function ProcessSave() {
    // Step 1: Read View Data and Create JSON Object

    var userModel = { "User": "", "Name": ""};

    var regionModel = {"Region" : "","Name": "", "Users": []};

    var processModel = { "User": "", "Description": "", "Code": "", "Regions": []};

    var oTable = $('.tbl').dataTable().fnGetData();

        for (var i = 0; i < oTable.length; i++) {

            regionModel.Name  = oTable[i][0];

            userModel.User    = oTable[i][1];
            userModel.Name    = oTable[i][1];

            regionModel.Users.push(userModel);
            processModel.Regions.push(regionModel);

            userModel   = { "Id": "", "Name": ""};
            regionModel = { "Name": "", "Users": []};
       }
    // Step 1: Ends Here

    // Set 2: Ajax Post
    // Here i have used ajax post for saving/updating information
    $.ajax({
        url: '/Process/Create',
        data: JSON.stringify(processModel),
        type: 'POST',
        contentType: 'application/json;',
        dataType: 'json',
        success: function (result) {

            if (result.Success == "1") {
                window.location.href = "/Process/Index";
            }
            else {
                alert(result.ex);
            }
        }
    });
}

型号

 namespace TestingTool.ViewModels
    {
        public partial class ProcessModel
        {
            public string Name { get; set; }
            public string Description { get; set; }
            public string Code { get; set; }

            public virtual ICollection<RegionModel> Regions { get; set; }
        }
    }

    namespace TestingTool.ViewModels
    {
        public class RegionModel
        {
            public int Region { get; set; }
            public string Name { get; set; }
            public virtual ICollection<UserModel> Users { get; set; }

        }
    }

    namespace TestingTool.ViewModels
    {
        public class UserModel
        {
            public int User{ get; set; }
            public string Name { get; set; }
        }
    }

HTML 输出

<table class="tbl" id="tbl">
            <thead>
                <tr>
                    <th>
                        Region
                    </th>
                    <th>
                        Owner
                    </th>
                </tr>
            </thead>
            <tbody>
                    <tr>
                        <td>
                            Belgium
                        </td>
                        <td>
                            <select id="item_Users" name="item.Users"><option value="1">Steven Segers</option>
<option value="2">Rui Martins</option>
</select>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            France
                        </td>
                        <td>
                            <select id="item_Users" name="item.Users"><option value="1">Steven Segers</option>
<option value="2">Rui Martins</option>
</select>
                        </td>
                    </tr>
            </tbody>
        </table>

最佳答案

你尝试过吗:

$("#item_Users option:selected").index();

如果您有许多具有相同 id 的选择,请尝试此操作(该选择器选择所有名称属性以 Users 结尾的“选择”:

 $("select[name$='Users']:eq(0) option:selected").index();  //get first select

 $("select[name$='Users']:eq(1) option:selected").index();  //get second select

但是在页面上使用多个相同 ID 的不良做法。页面上应该有相同的 id。但即使页面上有多个 id,最好为所有选择指定相同的类,在这种情况下,代码将很快:

 $("select.Users:eq(0) option:selected").index();  //get first select

 $("select.Users:eq(1) option:selected").index();  //get second select

更新:

$("select.Users").each(function (index, element) {
    var optionIndex = $(element).find("option:selected").index();
});

关于javascript - 如何使用javascript获取下拉列表的选定值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13509459/

相关文章:

javascript - 使用 ng-repeat 动态创建对象 - 想要默认选中复选框

javascript - TS2307 : Cannot find module './images/logo.png'

javascript - 如何在下拉列表中设置默认值 javascript

javascript - 在 AngularJS 中映射现有数据

asp.net-mvc - Elmah 未记录 404(缺少文件/图像)

c# - 如何在 Html.DropDownListFor 上选择显示的文本

c# - 如何正确地将 DropDownList 的 SelectedValue 从 View 发送到 Controller ? View 模型

javascript - 代理 Angular 请求与位于 azurewebsites 的服务 Node

php - 调用 PHP 函数并传递 JavaScript 变量

c# - 在来自不同列表的多个表上显示列标题