0
function sendValues() {
var str = $("#ryan_m").serialize();
var response = $('input[name=brand[]]:checked').val();
$.ajax({
    url: "/trying1.php?avoidcache=' + myTimestamp();",
    data: {str}
    cache: false
});
}

<form action="trying1.php?b_id=$brand_id" method="get"/></td>
     <input type="checkbox" name="brand[]" value="$brand_id"/>
         <input type="submit" value="Compare" id="ryan_m" method="get" />
            </form>

I am trying to use this Ajax request to execute upon checking the available check boxes. Currently, This script will do exactly what I want, but I have to hit submit for it to execute.

My question:

Is there a simple way that I can execute the ajax upon clicking the check box, without hitting submit?

1
  • Your problem is solved. Please select one working answer as a solution. Commented Apr 3, 2011 at 20:41

4 Answers 4

1
$("input[type=checkbox]").change(function() {
  $("form").submit();
});
Sign up to request clarification or add additional context in comments.

2 Comments

What is target? ;) And what if the checkbox is (un)ticked by keyboard?
target was meant to be filled in by the user. He didn't add a class to his checkboxes so I just put target. Anyway RPM I edited the code to use input with type checkbox. You can try that. Also .click might need to be .change.
0

You can use jQuery .submit() to submit the form on click.

<form action="trying1.php?b_id=$brand_id" id="myForm" method="get"/></td>
    <input type="checkbox" name="brand[]" class="myCheckbox" value="$brand_id"/>
    <input type="submit" value="Compare" id="ryan_m" method="get" />
</form>

Give you form an ID and give the checkbox a class or if you want an ID if there's only one. Then in your jquery.

function sendValues() {
    var str = $("#ryan_m").serialize();
    var response = $('input[name=brand[]]:checked').val();
    $.ajax({
        url: "/trying1.php?avoidcache=' + myTimestamp();",
        data: {str}
        cache: false
    });
 }

$('.myCheckbox').change(function(){
    $('#myForm').submit(function(){
        sendValues();
        return false; 
    });
});

I haven't tested out the code, but thats the idea you going for.

2 Comments

Why firing unnecessary events?
This does not work, In fact, It does not call the Page at all without clicking the submit button, which is what I do not want to do.
0
$(document).ready(function() {
    $('[name="brand[]"]').change(function() {
        sendValues();  
    });
});

Demo here: http://jsfiddle.net/JtBe2/

1 Comment

You're only detecting clicks. BUt no changes via keyboard or other scripts.
0
$("input[name=brand[]]").change(function() {
    sendValues();
    alert("Data is sent."); // Remove this - Just for testing purpose
});

jQuery Change Event. This detects all kinds of changes to all kinds of <input>. <select> and <textarea>.

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.