I have this jQuery code:
jQuery( document ).ready( function( $ ) {
$('#myform').submit( function() {
if ($('#myform input').is(':checked')) {
} else {
//alert('Checkbox is not checked!!');
window.location.replace("http://example.com/product-comparisons/?product-skus=0");
}
});
});
I want to redirect to this URL:
http://example.com/product-comparisons/?product-skus=0
If the user clicks the check box without any checked items.
I tried the above code based on this suggestion: How to redirect to another webpage in JavaScript/jQuery?
But that won’t work. I would appreciate for any inputs and suggestions. Thank you!
UPDATE: This is my HTML COde/a form:
<form action="/product-comparison/" id="myform">
<ul>
<li>
<input type="checkbox" name="product-skus[]" value="1" id="mycheckbox">
<a href="http://example.com/product/translator/">Translator</a>100<img width="150" height="150" title="View product" style="" alt="View product" class="attachment-thumbnail" src="http://example.com/wp-content/uploads/2012/12/pic1-150x150.jpg"></li>
<li><input type="checkbox" name="product-skus[]" value="2" id="mycheckbox">
<a href="http://example.com/product/arrows/">Arrows</a>120<img width="150" height="150" title="View product" style="" alt="View product" class="attachment-thumbnail" src="http://example.com/wp-content/uploads/2012/12/pic2.jpg"></li></ul>
<input type="submit" value="Compare">
</form>
However I believe my selector is correct because this diagnostic code (using alerts to make sure jQuery funnels to the right logic) is perfectly working:
jQuery( document ).ready( function( $ ) {
$('#myform').submit( function() {
if ($('#myform input').is(':checked')) {
alert('Checkbox is checked, do nothing');
} else {
alert('Checkbox is not checked!!, do the redirects');
}
});
});
if you submit your form, you submit to another document. This means in order to get to another location you can either set it by
document.location.replaceAND cancel the form submission (return false;should be enough )or you manipulate the action (target url) of your form. Positive side-effect would be, that any data typed into your form would actually be submitted in both cases (default and alternate action)