简体   繁体   English

在JQuery中找到动态添加的局部视图范围内容的控件

[英]Find the control of dynamically added partial view span content in JQuery

We have partial view View1 as follows: 我们有部分视图View1,如下所示:

@using (Html.BeginCollectionItem)
{
    <div id = "partialview-content>
        <table >
            <tr>
                <td>
                    @Html.TextBoxFor(x=>x.Name, new {id = "name", @class = "name-class"})
                    // Additional controls
                </td>
            </tr>
             <tr class="rowSpace">
                <td>Label Text</td>
                 <td>
                    <span id="business-key-id">
                        @Html.DisplayFor(x=>x.BusinessKey)
                    </span>
                </td>
           </tr>
       </table>
    }
</div>

When user clicks on Add New Item in Main View, partial view will be added dynamically. 当用户在主视图中单击“添加新项”时,将动态添加部分视图。

When user enters some text in name textbox in the dynamically added partial view, BusinessKey DisplayFor should be updated dynamically. 当用户在动态添加的局部视图的名称文本框中输入一些文本时,BusinessKey DisplayFor应该动态更新。

So I added below code in the JQuery: 因此,我在JQuery中添加了以下代码:

$(document).ready(function () {

    $(".name-class").live('keyup', function (event) {

        var name1 = $("#name1").val();
        var name2 = $("#name2").val();

        var name = $(this).val();

        (".business-key-id").html(trustName + ' ' + seriesName + ' ' + trancheName); 
    });    
});

Doing this code is updating the business-key-id of all the dynamically added partial views. 这段代码正在更新所有动态添加的局部视图的business-key-id。 How can I get the control of the business-key specific to the keydown event control? 如何获得特定于keydown事件控件的业务密钥的控件?

This line is selecting all elements with the class name business-key-id 该行选择所有具有类名称business-key-id的元素

$(".business-key-id").html(...

Change the selector to select only the next element 更改选择器以仅选择下一个元素

$(".name-class").on(...
  $(this).next('.business-key-id').html(...

Note .live has been depreciated - you should use .on 注意.live已被贬值-您应该使用.on

Edit 编辑

Based on OP's revised html, the selector to choose the coresponding business-key-id should be 根据OP的修订版html,用于选择与之相关的business-key-id的选择器应为

$(this).closest('table').find('.business-key-id).html(...

And change the html to use the class attribute, not id attribute (duplicate ID's are invalid html) 并更改html以使用class属性,而不是id属性(重复的ID是无效的html)

<span class="business-key-id">
  @Html.DisplayFor(x=>x.BusinessKey)
</span>

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

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