HTML
<li class="leaf">
// The span is my custom checkbox that wraps around the default checkbox.
<span class="checkbox facetapi-checkbox checked">
// The span above replaces the default checkbox below, which you'll notice has a display:none; I don't want both checkboxes showing.
<input type="checkbox" class="facetapi-checkbox" checked="true" style="display: none; ">
</span>
// This is the anchor tag that I need to click when my custom checkbox is checked.
// It's outside of my custom checkbox span, but shares the same parent.
<a href="/part-finder/field_rep_catalog/heater-1527" rel="nofollow" class="facetapi-active facetapi-checkbox-processed" style="display: none; ">(-)
<span class="element-invisible">Remove Support filter</span>
</a>
</li>
這裏是jQuery的使用,我自定義的一個來替換默認複選框:
//Checkbox Image Replacement
$('.faceted_search_column input[type=checkbox]').each(function() {
var span = $('<span class="' + $(this).attr('type') + ' ' + $(this).attr('class') + '"></span>').click(doCheck).mousedown(doDown).mouseup(doUp);
if ($(this).is(':checked')) {
span.addClass('checked');
}
$(this).wrap(span).hide();
});
// You'll notice that when this function is run, I've attempted to use .closest() to find the nearest anchor and then do a .triggerHandler('click') to click that anchor it finds. This obviously didn't work. Should I use .parent() to move back in the DOM?
function doCheck() {
if ($(this).hasClass('checked')) {
// This was my attempt to click the anchor when doCheck function is run.
$(this).closest('a.facetapi-checkbox-processed').triggerHandler('click');
$(this).removeClass('checked');
$(this).children().prop("checked", false);
} else {
$(this).parents('a.facetapi-checkbox-processed').triggerHandler('click');
$(this).addClass('checked');
$(this).children().prop("checked", true);
}
}
function doDown() {
$(this).addClass('clicked');
}
function doUp() {
$(this).removeClass('clicked');
}
當用戶切換複選框時,你們推薦哪種方式作爲最有效的方式觸發錨點?這是一個分面搜索錨,所以我需要選中或不選中才能觸發相同的錨標記。
在此先感謝那些花時間幫助我的人。
這沒有奏效。我也嘗試使用.find('a')來查看這是否可行,但沒有運氣:( – HoldTheLine
@HoldTheLine查看更新,查找('a')沒有返回,或者點擊沒有做任何事情? – Rob
你是男人!非常感謝你提供的豐富答案。儘管我無法用你提供的答案取得成功,但是你讓我走上了正確的道路,我能夠通過$(this).closest( 'li')。find('a')。attr('href');非常感謝你Rob! – HoldTheLine