javascript - jQuery .click event runs on page load -


i have jquery script in external file. im using update database. i've been practicing around , cannot right. .ajax call ran when button (labeled id "add") clicked, database updated everytime page refreshed , not when button clicked! can explain why? in advance

$('#add').click(     $.ajax({         url: 'test.php',         success: function() {             alert("added");         }     }) ); 

update: @flo able working perfectly. here's final product:

$(document).on('click','#add',function() {     $.ajax({         url: 'test.php',         success: function() {         alert("added");         }     }) }); 

i'm not 100% sure it, guess trigerring click stuff execute, instead of binding event !

i mean:

$('#add').click($.ajax...) 

should not same as:

$('#add').click(function (clickevent) {     // update here !     $.ajax... }); 

but anyway, should prefer:

$('#add').on('click', function (clickevent) {     // update here !     $.ajax... }); 

or better:

$(document).on('click', '#add', function (clickevent) {     // update here !     $.ajax... }); 

Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

jquery - Fancybox - apply a function to several elements -

An easy way to program an Android keyboard layout app -