How to make jQuery form submission not refresh the page but add the content - AJAX like? -
in $('#my_form').submit(function()
have code should add more information on selected song my_form
form. able change content of info_table
on each form submission (if different song marked). how can ? if leave return false;
page won't reload anymore.
<!doctype html> <html> <body> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js" ></script> <form id="my_form"> <table border='2' id="my_table"></table> <input type="submit" value="show song info" id="submit_button"> </form> <table border='2' id="info_table"></table> <script> var my_xml; $(document).ready(function() { $.get("songs_2.xml", function(d){ my_xml = d; //$('body').append("<table border='2'>"); var html = ""; var = 0; $(d).find('song').each(function(){ var $song = $(this); var title = $song.find('title').text(); var artist = $song.find('artist').text(); var cover = $song.find('cover').text(); var id = $song.find("id").text(); html += "<tr><td>"+id+"</td><td width='120'><b>" + artist + " </b></td><td width='150'> " + title + "</td><td>"; html += "<img src='" + cover + "' height='50' width='50'/></td><td> <input type='radio' name='which' value='"+id+"'></td></tr>" ; $('#my_table').append($(html)); html = "" i++; }); }); }); $('#my_form').submit(function() { var = $("#my_form input[type='radio']:checked").val(); $.get("songs_2.xml", function(d){ my_xml = d; //$('body').append("<table border='2'>"); var html = ""; var = 0; $(d).find('song').each(function(){ var $song = $(this); var title = $song.find('title').text(); var artist = $song.find('artist').text(); var cover = $song.find('cover').text(); var id = $song.find("id").text(); $('#info_table').html("<tr><td>" + alotofinfo + "</td></tr>"); }); }); return false; }); </script> </body> </html>
you need prevent default behavior of form submit
, modified code consider performance of calling jquery.html in each loop will add 1 row
$('#my_form').submit(function(e) { e.preventdefault(); var = $("#my_form input[type='radio']:checked").val(); $.get("songs_2.xml", function(d){ my_xml = d; var html = ""; $(d).find('song').each(function(){ var $song = $(this); var title = $song.find('title').text(); var artist = $song.find('artist').text(); var cover = $song.find('cover').text(); var id = $song.find("id").text(); html = html+"<tr><td>" + alotofinfo + "</td></tr>"; }); $('#info_table').append(html); }); });
i hope can :)
Comments
Post a Comment