javascript - Integrate POST method into dynamically created form -
i'm creating gui form insert object div apply classes animate across screen. way have set up, whenever select class , apply , hit submit button, seems refreshing whole page. know has using post method i'm not sure how. here js:
////////////////////////////////////////////////////////////////////////////////////////////////////////add sprite var spriteid = 1; $(".add_sprite").click(function() { $("<div />", { "class":"sprite_container", id:"sprite_container"+spriteid }) .append($("<div />", { "class":"sprite" , id:"sprite"+spriteid })) .appendto("#divmain"); spriteid++; }); ////////////////////////////////////////////////////////////////////////////////////////////////////////add sprite controls var controlsid = 1; $(".add_sprite").click(function() { $("<form />", { "class":"sprite_controls", id:"sprite_controls"+controlsid }) //sprite name .append($("<input />", {"class":"sprite_name", type: "text", value: "name it", id:"name"+controlsid })) //sprite animation .append($("<select/>", { "class":"sprite_animationa", id:"animationa"+controlsid }) .append($("<option />", { value:"animate it"}) .append("animate it") ) .append($("<option />", { value:"pulse"}) .append("pulse") ) .append($("<option />", { value:"star"}) .append("star") ) .append($("<option />", { value:"square"}) .append("square") ) ) .append($("<button/>", { "class":"run_it", id:"run_it"+controlsid }) .append("run it") ) .appendto("#controls"); controlsid++; }); ////////////////////////////////////////////////////////////////////////////////////////////////////////apply animations sprite 1 //$('#sprite_controls1').submit(applyanima1); //$('#run_it1').off().click(function() {$('#sprite_controls1').submit();}); // function applyanima1() { $('#run_it1').click(function (e) { var anima1 = $('#animationa1'); e.preventdefault(); if (anima1.val() == 'pulse'){ $("#sprite_container1").addclass("pulse"); } else if (anima1.val() == 'star'){ $("#sprite_container1").addclass("star"); } else if (anima1.val() == 'square'){ $("#sprite_container1").addclass("square"); } else{ } //} });
and here js fiddle: http://jsfiddle.net/2vazw/
any appreciated
i have changed code little bit things more efficiently.
first, used event delegation, means there no need add more listeners during execution. event propagates until reaches form , clicked button identified , event handled.
$('#formcontainer').delegate('button','click',function (e) { ...
i selected animation , index , there can whatever want.
the form separated rest of page elements, , there 1 form, not 1 form per sprite
.
on side note, if myself, separate logical representation of 'back-end' (i.e, code drives animator) ui accompanying creating animator class can instantiated , creates entire interface scratch , reusable , self-contained.
see example here.
Comments
Post a Comment