javascript - Why some ajax buttons not working? -


as can see, have 5 buttons. working good.

<button type="submit" id="submit_button1">img1</button> <button type="submit" id="submit_button2">img2</button> <button type="submit" id="submit_button3">img3</button> <button type="submit" id="submit_button4">img4</button> <button type="submit" id="submit_button5">img5</button> 

script.js:

function button(name,url) { $(name).click(function(){    $.ajax({       url:url+'.php',       success: function(html){       $("#show").after(html);       }    }); return false; }); }  $(document).ready(function(){     button(submit_button1,'action1');     button(submit_button2,'action2');     button(submit_button3,'action3');     button(submit_button4,'action4');     button(submit_button5,'action5'); }); 

by using same script.js, following works too:

<button type="submit" id="submit_button1">img1</button> <button type="submit" id="submit_button2">img2</button> 

but if use this:

<button type="submit" id="submit_button1">img1</button> <button type="submit" id="submit_button3">img3</button> 

button1 works, button3 not. don't know why.

or this:

<button type="submit" id="submit_button3">img3</button> <button type="submit" id="submit_button4">img4</button> 

button3 , button4 not work.

looks have use buttons if add them in script.js, if skip one, buttons not work. how solve problem?

you have 2 problems :

1 )

why invoking button(submit_button1,'action1'); , not button('submit_button1','action1');

2) function should treat id :

function button(name,url) { $('#'+name).click(function(){    $.ajax({ ... 

also - way can :

button($('#submit_button1'),'action1');

and function :

function button(name,url) { $(name).click(function(){ ... //$ redundant anyway... 

Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

node.js - Using Node without global install -

php - CakePHP HttpSockets send array of paramms -