简体   繁体   English

如何在具有#as href且在表单内提交的链接上更改URL参数

[英]How to change a URL parameter on a link that has # as href and is submitted inside a form

I'm working on a drupal module and it gets images from an API, adds selectable search-filters and generates pagination. 我正在研究一个drupal模块,它从API获取图像,添加可选的搜索过滤器并生成分页。 If I go to eg the 7th pagination page and add a filter that only spans 3 pages it will result in not showing anything at all. 例如,如果我转到第7页分页并添加仅跨越3页的过滤器,则将导致根本不显示任何内容。

Solution: on upon adding a filter, go back to the 1st page. 解决方案:在添加过滤器后,返回第一页。 Now is my problem that that page is an url parameter and the links made by the filter are set to href='#' so basically this page yes. 现在是我的问题,该页面是一个url参数,并且过滤器建立的链接设置为href='#'因此基本上该页面是。

So I try to debug the javascript that handles the on click: 因此,我尝试调试处理on click的javascript:

$('#blabla .filter-url').click(function(e) {
    e.preventDefault();

    var link = $(e.currentTarget);
    console.log(link);
    var filter_key = link.data('filter-key');
    var filter_value = link.data('filter-value');
    // var active = link.hasClass('active');

    var filters_input = $('#blabla input[name="filters"]');

    var current_filters = JSON.parse(filters_input.val() || '{}');
    current_filters.filters = current_filters.filters || [];

    var exists = ($.grep(current_filters.filters, function(e, i){
        return e.key == filter_key;
    }).length);
    if (exists) {
         //Remove current filter from list
        current_filters.filters = $.grep(current_filters.filters, function(e, i){
            return e.key != filter_key;
        });
        link.removeClass('active');
    }

    // Add current filter to list
    current_filters.filters.push({
        key: filter_key,
        value: filter_value,
    });
    link.addClass('active');

    filters_input.val(JSON.stringify(current_filters));
    $('#formname').submit();
});

the link object: 链接对象:

链接对象

The link object has a {0, context and length} object in which both 0 and context contain multiple instances of the url, which one should I edit? 链接对象有一个{0, context and length}对象,其中0context都包含URL的多个实例,我应该编辑哪一个?

I just want to manipulate the URL / get parameter page=xxx to be page=0 , so it goes back to the 1st page. 我只想操纵URL /获取参数page=xxxpage=0 ,所以它可以返回到第一页。 How to achieve this? 如何实现呢?

I was searching in the wrong place. 我在错误的位置搜索。 At the end of the onclick-function it submits the form. 在onclick函数的最后,它提交表单。 I had to change the action of this form like this: 我必须像这样更改此表单的action

var old_form_action = $('#formname').attr('action');
var new_form_action = old_form_action.replace(/(page=)[^\&]+/, '$1' + '0');
$('#formname').attr('action', new_form_action);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM