admin管理员组文章数量:1399482
I would like to add an onclick event to the Opencart filters to replace the submit button which is hidden off the page.. I am presuming that this should be done with javascript/jquery, but implementation is a little beyond me, can you help?
PHP:
<div class="box">
<div class="box-heading"><?php echo $heading_title; ?></div>
<div class="box-content">
<ul class="box-filter">
<?php foreach ($filter_groups as $filter_group) { ?>
<li><span id="filter-group<?php echo $filter_group['filter_group_id']; ?>"><?php echo $filter_group['name']; ?></span>
<ul>
<?php foreach ($filter_group['filter'] as $filter) { ?>
<?php if (in_array($filter['filter_id'], $filter_category)) { ?>
<li>
<input type="checkbox" value="<?php echo $filter['filter_id']; ?>" class="click_checkbox_manufacturers-filter" id="filter<?php echo $filter['filter_id']; ?>" checked="checked" />
<label for="filter<?php echo $filter['filter_id']; ?>"><?php echo $filter['name']; ?></label>
</li>
<?php } else { ?>
<li>
<input type="checkbox" value="<?php echo $filter['filter_id']; ?>" class="click_checkbox_manufacturers-filter" id="filter<?php echo $filter['filter_id']; ?>" />
<label for="filter<?php echo $filter['filter_id']; ?>"><?php echo $filter['name']; ?></label>
</li>
<?php } ?>
<?php } ?>
</ul>
</li>
<?php } ?>
</ul>
<a id="button-filter" class="button"><?php echo $button_filter; ?></a>
</div>
</div>
HTML OUTPUT:
<div class="box">
<div class="box-heading">Refine Search</div>
<div class="box-content">
<ul class="box-filter">
<li><span id="filter-group2">Colour</span>
<ul>
<li>
<input type="checkbox" value="33" class="click_checkbox_manufacturers-filter" id="filter33">
<label for="filter33">Black</label>
</li>
<li>
<input type="checkbox" value="35" class="click_checkbox_manufacturers-filter" id="filter35">
<label for="filter35">Blue</label>
</li>
<li><span id="filter-group4">Scent Name</span>
<ul>
<li>
<input type="checkbox" value="64" class="click_checkbox_manufacturers-filter" id="filter64">
<label for="filter64">Almond</label>
</li>
<li>
<input type="checkbox" value="65" class="click_checkbox_manufacturers-filter" id="filter65">
<label for="filter65">Coconut</label>
</li>
</ul>
</li>
</ul>
<a id="button-filter" class="button">Refine Search</a>
</div>
</div>
JavaScript
<script type="text/javascript">
$('#button-filter').bind('click', function() {
filter = [];
$('.box-filter input[type=\'checkbox\']:checked').each(function(element) {
filter.push(this.value);
});
location = '<?php echo $action; ?>&filter=' + filter.join(',');
});
//--></script>
I would like to add an onclick event to the Opencart filters to replace the submit button which is hidden off the page.. I am presuming that this should be done with javascript/jquery, but implementation is a little beyond me, can you help?
PHP:
<div class="box">
<div class="box-heading"><?php echo $heading_title; ?></div>
<div class="box-content">
<ul class="box-filter">
<?php foreach ($filter_groups as $filter_group) { ?>
<li><span id="filter-group<?php echo $filter_group['filter_group_id']; ?>"><?php echo $filter_group['name']; ?></span>
<ul>
<?php foreach ($filter_group['filter'] as $filter) { ?>
<?php if (in_array($filter['filter_id'], $filter_category)) { ?>
<li>
<input type="checkbox" value="<?php echo $filter['filter_id']; ?>" class="click_checkbox_manufacturers-filter" id="filter<?php echo $filter['filter_id']; ?>" checked="checked" />
<label for="filter<?php echo $filter['filter_id']; ?>"><?php echo $filter['name']; ?></label>
</li>
<?php } else { ?>
<li>
<input type="checkbox" value="<?php echo $filter['filter_id']; ?>" class="click_checkbox_manufacturers-filter" id="filter<?php echo $filter['filter_id']; ?>" />
<label for="filter<?php echo $filter['filter_id']; ?>"><?php echo $filter['name']; ?></label>
</li>
<?php } ?>
<?php } ?>
</ul>
</li>
<?php } ?>
</ul>
<a id="button-filter" class="button"><?php echo $button_filter; ?></a>
</div>
</div>
HTML OUTPUT:
<div class="box">
<div class="box-heading">Refine Search</div>
<div class="box-content">
<ul class="box-filter">
<li><span id="filter-group2">Colour</span>
<ul>
<li>
<input type="checkbox" value="33" class="click_checkbox_manufacturers-filter" id="filter33">
<label for="filter33">Black</label>
</li>
<li>
<input type="checkbox" value="35" class="click_checkbox_manufacturers-filter" id="filter35">
<label for="filter35">Blue</label>
</li>
<li><span id="filter-group4">Scent Name</span>
<ul>
<li>
<input type="checkbox" value="64" class="click_checkbox_manufacturers-filter" id="filter64">
<label for="filter64">Almond</label>
</li>
<li>
<input type="checkbox" value="65" class="click_checkbox_manufacturers-filter" id="filter65">
<label for="filter65">Coconut</label>
</li>
</ul>
</li>
</ul>
<a id="button-filter" class="button">Refine Search</a>
</div>
</div>
JavaScript
<script type="text/javascript">
$('#button-filter').bind('click', function() {
filter = [];
$('.box-filter input[type=\'checkbox\']:checked').each(function(element) {
filter.push(this.value);
});
location = '<?php echo $action; ?>&filter=' + filter.join(',');
});
//--></script>
Share
Improve this question
edited Oct 28, 2013 at 9:24
Stuart
asked Oct 27, 2013 at 16:51
StuartStuart
6882 gold badges10 silver badges27 bronze badges
2 Answers
Reset to default 4You can do something like this (instead of the current script):
<script type="text/javascript">
$(document).ready(function() {
// hide the "submit" button
$('#button-filter').hide();
// bind onChange event to the checkboxes
$('.click_checkbox_manufacturers-filter').live('change', function() {
filter = [];
$('.box-filter input[type=\'checkbox\']:checked').each(function(element) {
filter.push(this.value);
});
window.location = '<?php echo $action; ?>&filter=' + filter.join(',');
});
});
//--></script>
Here is link to my JsFiddle of the very same JS code, which, surprisingly is working. The only issue may be with redirect... I edited the redirect code above so it should work as well.
For version 2 change the JavaScript in the filter.tpl file (from catalog/view/theme/default/template/extension/module/filter.tpl - change 'default' to your theme name if necessary) to this
<script type="text/javascript">
$(document).ready(function() {
$('#button-filter').hide();
$('input[name^=\'filter\']').on('change', function() {
filter = [];
$('input[name^=\'filter\']:checked').each(function(element) {
filter.push(this.value);
});
location = '<?php echo $action; ?>&filter=' + filter.join(',');
});
});
</script>
Tested in OC 2.3.0.2.
本文标签: javascriptOpencart Filters add onclick event to replace the submit buttonStack Overflow
版权声明:本文标题:javascript - Opencart Filters add onclick event to replace the submit button - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744188611a2594410.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论