gpt4 book ai didi

javascript - 当我的自定义复选框被切换时如何单击最近的 anchor

转载 作者:行者123 更新时间:2023-12-02 19:22:08 25 4
gpt4 key购买 nike

Faceted search with Custom Checkbox

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');
}

你们建议当用户切换复选框时触发 anchor 点击的最有效方法是什么?它是一个多面搜索 anchor ,因此我需要选中或取消选中才能触发相同的 anchor 标记。

预先感谢那些花时间帮助我的人。

最佳答案

最近仅查看元素的直接祖先。您可以结合使用closest('li') 和find('a') 来实现您的目标。

 $(this).closest('li').find('a.facetapi-checkbox-processed').click();

TriggerHandler 不会在 dom 树中冒泡,因此它会尝试仅触发 Anchor 上的点击,而 Anchor 标签上没有绑定(bind)任何触发器。使用触发器('click')或简单地使用 .click() 将事件传播到跨度上的事件处理程序,或更改查找以直接查找跨度。

关于javascript - 当我的自定义复选框被切换时如何单击最近的 anchor ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12428598/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com