简体   繁体   English

发送html.dropdownlist中表中当前项目的ID以进行onchange

[英]Send id of current item in table in html.dropdownlistfor onchange

Im trying to get the @item.Id sent to the JavaScript function with @onChange. 我正在尝试使用@onChange将@ item.Id发送到JavaScript函数。 But when I write it like the code below then the JavaScript function reads it as a plain text. 但是,当我像下面的代码一样编写代码时,JavaScript函数会将其读取为纯文本格式。

Razor page: 剃刀页面:

<table class="table-striped">

    <thead>
        <tr class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
            <th class="col-lg-1 col-md-1 col-sm-1 col-xs-1">
                <i class="fa fa-sort" title="Blocknummer"></i>
            </th>
            <th class="col-lg-4 col-md-4 col-sm-4 col-xs-4">
                <i class="fa fa-tag" title="Namn på block"></i>
            </th>
            <th class="col-lg-2 col-md-2 col-sm-2 col-xs-2">
                <i class="fa fa-clock-o" title="Längd på block"></i>
            </th>
            <th class="col-lg-5 col-md-5 col-sm-5 col-xs-5">
                <i class="fa fa-user" title="Extern aktör"></i>
            </th>
        </tr>
    </thead>
    <tbody>
        @foreach (var item in Model.CourseBlocks)
            {
            <tr class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
                <td class="col-lg-1 col-md-1 col-sm-1 col-xs-1">
                    @Html.DisplayFor(m => item.BlockOrder)
                </td>
                <td class="col-lg-4 col-md-4 col-sm-4 col-xs-4">
                    @Html.DisplayFor(m => item.BlockName)
                </td>
                <td class="col-lg-2 col-md-2 col-sm-2 col-xs-2">
                    @Html.DisplayFor(m => item.DisplayLength)
                </td>
                <td class="col-lg-5 col-md-5 col-sm-5 col-xs-5">
                    @if (item.NeedExternActor)
                    {
                        @Html.DropDownListFor(model => item.ExternActorId, new SelectList(Model.ContactPersons, "Value", "Text"), new { @class = "form-control form-control-280 input-sm", @onChange = "actorChange('@item.Id', this.options[this.selectedIndex].value)" })
                    }
                </td>
            </tr>
        }
    </tbody>

</table>

JavaScript: JavaScript:

function actorChange(id, value) {

var itemId = id;
var actorId = value;
}

Thanks in advance for all help I can get. 在此先感谢您能获得的所有帮助。

采用

@onChange = "actorChange(@item.Id, this.value)"

After new attempts so I solved it by doing like this: 经过新的尝试,所以我通过这样解决了它:

<tbody>
    @foreach (var item in Model.CourseBlocks)
        {
        <tr id="@item.Id" class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
            <td class="col-lg-1 col-md-1 col-sm-1 col-xs-1">
                @Html.DisplayFor(m => item.BlockOrder)
            </td>
            <td class="col-lg-4 col-md-4 col-sm-4 col-xs-4">
                @Html.DisplayFor(m => item.BlockName)
            </td>
            <td class="col-lg-2 col-md-2 col-sm-2 col-xs-2">
                @Html.DisplayFor(m => item.DisplayLength)
            </td>
            <td class="col-lg-5 col-md-5 col-sm-5 col-xs-5">
                @if (item.NeedExternActor)
                {
                    @Html.DropDownListFor(model => item.ExternActorId, new SelectList(Model.ContactPersons, "Value", "Text"), new { @class = "form-control form-control-280 input-sm", @onChange = "actorChange(this)" })
                }
            </td>
        </tr>
    }
</tbody>

And in javascript: 并且在javascript中:

function actorChange(e) {

var itemId = $(e).closest('tr').attr('id');
var actorId = e.value;
}

然后试试这个

@Html.DropDownListFor(model => item.ExternActorId, new SelectList(Model.ContactPersons, "Value", "Text"), new { @class = "form-control form-control-280 input-sm", @id="@item.Id", @onChange = "actorChange(this.id, this..value)" })

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

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