开发者

how to create the div on the fly in javascript / jquery in this way

开发者 https://www.devze.com 2023-02-09 05:16 出处:网络
i have asp.net mvc 2 application where i have a scenario as follows: I want to create the parent div and in side it want to create n-number of divs with controls.like

i have asp.net mvc 2 application where i have a scenario as follows: I want to create the parent div and in side it want to create n-number of divs with controls.like

    for(j=0;j<parentCount;j++)
    {
    //Create the parent div
      for(i=0;i<childDivCount;i++)
    {
    //create child div, with controls (like radio,checkboxes).and attach to parentDiv
    }
//Attach parent div to document.
    }

how to achieve this .

Edited: i am bit get satisfaction with answers. but when i am using getJson in each loop as:

  //$.getJSON(url1,,function(data1){
    //for each item in data1 :-
         for(j=0;j<parentCount;j++)
              {
                  //Create the parent div 
                  //$.getJSON(url2,,function(data2){
//**** here j automatically get j++ , why ? ****//
                  //For each item in data2 :-
                  for(i=0;i<childDivCount;i++)
                 {
                     //create child div, with controls (like radio,checkboxes).and attach to     parentDiv
                  }

               });
            //Attach parent div to document.
                }
        });

when entering for getJson of data2 (subloop) var j get automatically incremented. why that should be ?

Edited:- in fact i want to create the question-answer scenario. since in first getJson call i am getting all questions and then开发者_运维知识库 in sub getJson call i am getting answers of each question. for each question i am creating div as parent div , and for its answers appending parent div with answers child div. thats all. i didn't wanted to use user control here , so i used the json-jquery combination to generate scenario. this is what i want to achieve.


This should do it :).

for (j=0;j<parentCount;j++)
{
  parentDiv = $('<div />');

  for (i=0;i<childDivCount;i++)
  {
    childDiv = $('<div />');
    parentDiv.append(childDiv);
  }

  someElementInDocument.append(parentDiv);
}


something like this should do the trick (haven't tried to run but it should be mostly right).

for(j=0;j<parentCount;j++)
{
    //Create the parent div
    $('body').append('<div id="parent-div">');
    for(i=0;i<childDivCount;i++)
    {
        //create child div, with controls (like radio,checkboxes).and attach to parentDiv
         $('#parent-div').append('<select>...</select>');
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消