简体   繁体   English

如何在jquery.each()函数中分配元素值

[英]How to assign element values inside jquery.each() function

I have a php page which calls a javascript function to populate it. 我有一个php页面,该页面调用javascript函数来填充它。 The page is a list of users each with their own form, the form id has the id appended to its name to allow edit, delete etc on each user. 该页面是一个用户列表,每个用户都有自己的表单,表单ID的ID后面附加了ID,以允许对每个用户进行编辑,删除等操作。 The form is is the shape of an html page that is in theory sucked in for each record. 表单是一个html页面的形状,从理论上讲,它是每个记录都吸收的页面。 I am using jquery each() to iterate over the records, then in theory the html page should be sucked in and populated for each record. 我正在使用jquery each()遍历记录,然后从理论上讲,应该为每个记录吸收并填充html页面。 I have tried for loop also. 我也尝试过循环。 The code below produces the correct number of forms but empty. 下面的代码生成正确数量的表单,但为空。 Other efforts have left me with only the last record populating the fist box and the others empty. 其他努力使我只有第一笔记录填入拳头盒,而其他记录则空着。 Any help greatly appreciated. 任何帮助,不胜感激。

$(Udata).each(function(uId, value){
    uid = value.uid;
    var uname = value.uname;
    function setValues() {
       window.document.getElementById("username").value=uname;
       window.document.getElementById("frmDelUsr").setAttribute("name","frmDelUsr"+uid);
    }

    $.get("users.html",{}, function (data) {                            
    $("#divfrmDelUser").append(data), function(){setValues();}

});
$(Udata).each(function(uId, value){
    uid = value.uid;
    var uname = value.uname;
    function setValues() {
       window.document.getElementById("username").value=uname;
       window.document.getElementById("frmDelUsr").setAttribute("name","frmDelUsr"+uid);
    }

    $.get("users.html",{}, function (data) {                            
    $(this).children('.divfrmDelUser').append(data), function(){setValues();} //<--- CHANGED

});

First you'd have to change the element with the ID "divfrmDelUser" by making that ID into a class(just erase "id" and type "class" obviously), then what this ought to do is select the current Udata being used, and then look for the elements within it containing the class "divfrmDelUser" and do what you set it to do past that. 首先,您必须通过将ID设置为一个类来更改ID为“ divfrmDelUser”的元素(只需清除“ id”并明显键入“ class”),然后要做的就是选择当前使用的Udata,然后在其中查找包含“ divfrmDelUser”类的元素,并进行设置以完成此操作。

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

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