1

I have the below code so it doesn't let my page reload in every submit.

Can I modify it so instead of the submit selector I use the name="value" and put multiple values?

I need that because I have submit buttons that are type="image" so I cannot use the submit selector.

e.g. if I have buttons with names={nam1, nam2, nam3} how do I change this code in order to get the page not refreshed when they are clicked?

$(".class123").click(function() {
  var keyValues = {
    pid : $(this).parent().find('input[name="pid"]').val(),
    };
      $.post('help_scripts/cart_functions.php', keyValues, function(rsp) {
       // make your php script return some xml or json that gives the result
       // rsp will be the response
       });
         return false;
         // so the page doesn't POST
 });

In this line - WHICH I NEED TO HAVE -

pid : $(this).parent().find('input[name="pid"]').val(),

pid is the name and id of my 1st button... How do I add more keyvalues here?

2 Answers 2

4

I think you can just do comma seperated selectors:

$(':submit').click(function() {
  var keyValues = {
    pid : $('input[name="nam1"],input[name="nam2"],input[name="nam3"]').val(),
    };
      $.post('help_scripts/cart_functions.php', keyValues, function(rsp) {
       // make your php script return some xml or json that gives the result
       // rsp will be the response
       });
         return false;
         // so the page doesn't POST
 });

http://api.jquery.com/multiple-selector/

That said, it might make more sense to assign a css class to all buttons you want to have this functionality or just select all buttons.

Button selector:

$(":button")

Class selector:

$(".SubmitButton")
Sign up to request clarification or add additional context in comments.

7 Comments

class sounds better... and how do I add more keyValues in the code? I saw the comma separated names but <pid> there is the name of my 1st button... I will modify my post a bit to express what I need if I use class selector.
"how do I add more keyValues in the code?" -- var keyValues = {nam1:$(input[name="nam1").val(), nam2:$(input[name="nam2").val()...etc }
also, when defining keyValues, get rid of the trailing comma if nothing follows it. IE6 (and maybe 7) will choke on it, if you care about browser compatibility.
@daybreaker ok... it did the trick... but it has a problem... when I press nam2=delete button which deletes an item, it does delete it but it needs the page to refresh to remove it. Is there a fix so when the delete button is pressed the item gets deleted immediately? thank you
I guess you could add another class that hides the data you want to disappear.
|
0

Change type submit to just a plain ole vanilla <button>.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.