简体   繁体   English

从Razor到jQuery获取动态创建的TextBox ID

[英]Get Dynamically Created TextBox ID From Razor To jQuery

I am trying to get the dynamically created TextBox ID into jQuery from Razor view. 我正在尝试从Razor视图将动态创建的TextBox ID放入jQuery。 The IDs are created in HTML as follows: 这些ID是用HTML创建的,如下所示:

Product - 1: cartDetails_0__Quantity
Product - 2: cartDetails_1__Quantity

Right now, when I give the above inputs directly to Ajax call, it updates the corresponding rows. 现在,当我将上述输入直接提供给Ajax调用时,它将更新相应的行。 As an example: 举个例子:

 @if (ViewBag.Cart != null)
        {
            for (int i = 0; i < cartDetails.Count(); i++)
            {
                <tr>
                    <td style="text-align: center;">@Html.TextBoxFor(model => cartDetails[i].Id)</td>
                    <td style="text-align: center;">@Html.DisplayFor(model => cartDetails[i].IP)</td>
                    <td style="text-align: center;">@Html.DisplayFor(model => cartDetails[i].ProductName)</td>
                    <td style="text-align: center;">@Html.DisplayFor(model => cartDetails[i].Price)</td>
                    <td style="text-align: center;">@Html.TextBoxFor(model => cartDetails[i].Quantity, new { @class = "quantityUpdate", data_id = cartDetails[i].Id })</td>
                </tr>
            }
        }

var url = '@Url.Action("UpdateCart2")';
$(".quantityUpdate").change(function () {
    var id = $(this).data('id');

    $('.quantityUpdate').each(function (i, item) {
        $.post(url, { id: id, quantity: $('#cartDetails_' + 0 + '__Quantity').val() }, function (response) { //cartDetails_0__Quantity - The first TextBox ID
            if (response) {
                $("#TotalPrice").load(window.location + " #TotalPrice");
            }
        });
    })
    alert($('#cartDetails_' + 0 + '__Quantity').val());
});

Is there any way to loop through jQuery to get the dynamically generated TextBox ID in Razor? 有没有办法遍历jQuery以在Razor中获取动态生成的TextBox ID? I've tried the following but doesn't get the value: 我尝试了以下操作,但没有获得价值:

 $('.quantityUpdate').each(function (i, item) {
        $.post(url, { id: id, quantity: $('#cartDetails_' + i + '__Quantity').val() }, function (response) { //cartDetails_0__Quantity - The first TextBox ID
            if (response) {
                $("#TotalPrice").load(window.location + " #TotalPrice");
            }
        });
    }) 

Even tried this one but it gets the value of first TextBox only: 即使尝试过此方法,但它仅获得第一个TextBox的值:

     $('.quantityUpdate').each(function (i, item) {
        $.post(url, { id: id, quantity: $(this).val() }, function (response) { //cartDetails_0__Quantity - The first TextBox ID
            if (response) {
                $("#TotalPrice").load(window.location + " #TotalPrice");
            }
        });
    }) 

Note : I am trying to update rows giving input to the TextBoxes with Ajax call. 注意 :我正在尝试更新通过Ajax调用向TextBox提供输入的行。 The TextBoxes are in a loop in the view. TextBoxes在视图中处于循环状态。 In this regards, I've to get the IDs of the dynamically generated HTML IDs. 在这方面,我必须获取动态生成的HTML ID的ID。

You can use create event of dynamically created elements in order to get their Ids. 您可以使用动态创建的元素的create事件来获取其ID。 But bear in mind to use this you need to use On(). 但是请记住要使用此功能,您需要使用On()。 See http://api.jquery.com/on/ 参见http://api.jquery.com/on/

See also: Event binding on dynamically created elements? 另请参阅: 在动态创建的元素上进行事件绑定? In jQuery, how to attach events to dynamic html elements? 在jQuery中,如何将事件附加到动态html元素?

PS. PS。 If there is a cleaner way to get dynamically created elements I would also be glad to get it :) 如果有一种更干净的方法来获取动态创建的元素,我也很高兴得到它:)

Edit. 编辑。 Maybe I was not clear enough . 也许我还不够清楚。 There is no exactly "create" event. 没有确切的“创建”事件。 You just can hook any actions you need to On() 您只需将所需的任何动作挂接到On()

See also jQuery "on create" event for dynamically-created elements 另请参见jQuery“创建时”事件,以动态创建元素

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

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