目前第二个元素只会在第一个元素之后发生变化。
是否可以在两个元素上使用相同的事件处理程序,如果它们都是下拉框并且在构建正确的 URL 时保持两个元素被选中,无论哪个元素接收到事件?
$(function(){
var url = '';
$('#search_region').change(function () {
url = $(this).val();
window.location.hash = url;
console.log(window.location.hash);
});
$('#search_categories').change(function () {
if(url !==''){
window.location.hash = url+"&"+$(this).val();
}
console.log(window.location.hash);
});
});
最佳答案
At the moment the second element is only ever changed after the first one is.
在这种情况下,您可以将类别的哈希值添加到区域
$('#search_region, #search_categories').change(function () {
var regURL = $('#search_region').val();
var catURL = $('#search_categories').val();
window.location.hash += regURL + ( catURL ? ("&" + catURL) : "" );
console.log(window.location.hash);
});
关于javascript - 如何为两个元素设置一个事件处理程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48623369/