Jquery selectors 如何在jQuery ON方法中绑定多对选择器:事件
我正在使用jQuery 1.7+最新方法,但未能实现,请帮助我 工作 基本上这是我的 HTML 现在我有以下问题/疑问 我们是否可以将一个事件与一个选择器绑定,即调用上述函数 更改selectbox或单击ul 如何在.on方法中设置数据参数。我试过下面的方法 $document。在“更改”按钮上,单击“ulsortByRight”,选择“排序”, {selectedOption:$'selectsort'.val,whatToShow:$this.attr'id'}, 函数{console.log'selectedOption:'+selectedOption+'whatToShow:'+whatToShow;} ; 但获取一个未定义selectedOption的错误 李,我们可以写这样的东西吗;因为我需要的是li的id而不是selectbox的id 如果有任何其他优化的解决方案使用的功能,如生活或绑定,那么请告诉我Jquery selectors 如何在jQuery ON方法中绑定多对选择器:事件,jquery-selectors,jquery,Jquery Selectors,Jquery,我正在使用jQuery 1.7+最新方法,但未能实现,请帮助我 工作 基本上这是我的 HTML 现在我有以下问题/疑问 我们是否可以将一个事件与一个选择器绑定,即调用上述函数 更改selectbox或单击ul 如何在.on方法中设置数据参数。我试过下面的方法 $document。在“更改”按钮上,单击“ulsortByRight”,选择“排序”, {selectedOption:$'selectsort'.val,whatToShow:$this.attr'id'}, 函数{console.lo
非常感谢。我不是100%清楚你到底想做什么,但有一件事没有多大意义。您希望在更改选择框时发送LI的ID。哪个李?最后一个李?您需要存储活动LI的状态,以便在选择框更改时可以在ajax请求中发送它 也许是这样的:
$('select#sort').change(function() {
processAjax();
});
$('ul#sortByRight > li > a').click(function() {
$(this).closest('ul').find('li').removeClass('active');
$(this).closest('li').addClass('active');
processAjax();
});
function processAjax() {
selectedOption = $('select#sort').val();
whatToShow = $('ul#sortByRight').find('.active').attr('id');
alert('selectedOption:' + selectedOption + 'whatToShow:' + whatToShow);
}
+1.谢谢你的帮助。我想缩短jquery事件。我已经知道上面的方法了。是的,我想要当前selectedclciked`li`的is。在方法上不是旧的。更改方法您不需要使用同一个选择器绑定两个事件。从下拉列表中选择项目时,您不希望代码选择要运行的活动LI。单击LI时,不需要将其绑定到change函数。您可以在事件中抛出一个条件来检查哪个元素调用了它,但说真的,您不需要这样做。
$(document).on('change click', 'ul#sortByRight,select#sort', function() {
selectedOption = $('select#sort').val();
whatToShow = $(this).attr('id');
alert('selectedOption:'+selectedOption+'whatToShow:'+whatToShow);
}
);
$('select#sort').change(function() {
processAjax();
});
$('ul#sortByRight > li > a').click(function() {
$(this).closest('ul').find('li').removeClass('active');
$(this).closest('li').addClass('active');
processAjax();
});
function processAjax() {
selectedOption = $('select#sort').val();
whatToShow = $('ul#sortByRight').find('.active').attr('id');
alert('selectedOption:' + selectedOption + 'whatToShow:' + whatToShow);
}