JQuery criar uma forma e adicionar elementos a ele programaticamente

Preciso de criar uma forma e adicionar elementos programaticamente.

$form = $("<form></form>");
$form.append("<input type=button value=button");
Isto não parece funcionar bem.

 23
Author: EBAG, 2010-12-21

5 answers

A segunda linha deve ser escrita como:

$form.append('<input type="button" value="button">');
 22
Author: Arvin, 2015-09-30 04:17:08

Você precisa anexar form A body também:

$form = $("<form></form>");
$form.append('<input type="button" value="button">');
$('body').append($form);
 37
Author: Sarfraz, 2015-09-30 04:17:51
var form = $("<form/>", 
                 { action:'/myaction' }
            );
form.append( 
    $("<input>", 
         { type:'text', 
           placeholder:'Keywords', 
           name:'keyword', 
           style:'width:65%' }
     )
);

form.append( 
     $("<input>", 
          { type:'submit', 
            value:'Search', 
            style:'width:30%' }
       )
);

$("#someDivId").append(form);
 20
Author: Sergio Viera, 2015-09-30 04:18:08
function setValToAssessment(id)
{

     $.getJSON("<?= URL.$param->module."/".$param->controller?>/setvalue",{id: id}, function(response)
     {
        var form = $('<form></form>').attr("id",'hiddenForm' ).attr("name", 'hiddenForm'); 
         $.each(response,function(key,value){
            $("<input type='text' value='"+value+"' >")
 .attr("id", key)
 .attr("name", key)
 .appendTo("form");


             });
              $('#hiddenForm').appendTo('body').submit();

        // window.location.href = "<?=URL.$param->module?>/assessment";
    });

}     
 5
Author: manoj Bhambere, 2015-09-30 04:18:33

A etiqueta não está fechada:

$form.append("<input type=button value=button");

Deve ser:

$form.append('<input type="button" value="button">');
 3
Author: Shurdoof, 2015-09-30 04:18:19