简体   繁体   English

JQuery.each() Function 为子元素分配相同的 ID(不是迭代?)

[英]JQuery .each() Function assigning the same ID for children elements (not iterating?)

I am trying to iterate through a list of children elements and pass their encrypted IDs via AJAX to my controller to update display order on drop using jQuery Sortable.我正在尝试遍历子元素列表,并通过 AJAX 将它们的加密 ID 传递到我的 controller,以使用 jQuery Sortable 更新放置时的显示顺序。 jQuery is selecting the correct amount of child elements but it is assigning the same ID from the dragged and dropped item to both items in my array. jQuery 正在选择正确数量的子元素,但它正在将拖放项目中的相同 ID 分配给我数组中的两个项目。 I'm not sure what's going on.. I would really appreciate the help!我不确定发生了什么......我真的很感激你的帮助! Thanks谢谢

JS Code: JS代码:

$(document).ready(function () {
       
        $(".reqconsentList").sortable({
            axis: 'y',
            update: function (event, ui) {

                var consentForms = [];

                $(this).children().each(function (index) {
                    consentForms.push({ 'id': $('#ConsentID').val(), 'position': index + 1 });
                 
                });
                
                var data = { 'sortedConsentForms': consentForms };
                //debugger;
                // POST to server using $.post or $.ajax
                $.ajax({
                    data: data,
                    type: 'POST',
                    url: '@Url.Action("UpdateConsentDisplayOrder", "Check", new { Area = "Administration" })'
                });
            }
        });
});

First problem is that $('#ConsentID').val() would return the value from the first element with that id.第一个问题是$('#ConsentID').val()会返回具有该 ID 的第一个元素的值。 ID needs to be Unique. ID 需要是唯一的。

Changing the jquery to class would be correct, but the code would still not know what "element" it should get the data from.将 jquery 更改为class是正确的,但代码仍然不知道应该从哪个“元素”获取数据。

So we need to tell it search for the .reqconsentList , to look for child with the class .ConsentID所以我们需要告诉它搜索.reqconsentList ,寻找 class .ConsentID的孩子

This is done this way: $(this).find('.ConsentID').val()这是通过这种方式完成的: $(this).find('.ConsentID').val()

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

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