How to clear the value of the parameter to enter new values?

Asked

Viewed 110 times

2

&fespecial=lanccontrucao&fespecial=novopronto

Here is a parameter that persists when executing the code below:

// $("#fe1").click(function(){
// $("#fe2").click(function(){

$("#fe3").click(function(){

    var url         = window.location.href;
    var parametro   = "fespecial";
    var valor       = $(this).val();
    var master      = url+"&"+parametro+"="+valor;

    if( $("#fe2").is(":checked") ){
        ///////////////////////////////////////
        // alert("Checked " +master);
        window.location.href = master;

    } else {
        ///////////////////////////
        // alert("Unchecked " +master); 

    }

});

I need to clear the current values of the parameter fespecial to insert a new value in this same parameter that is part of a url. Every time I click on one of these ids it inserts another parameter fespecial with a new value and I would like to clear what exists to enter the new value of this parameter.

1 answer

1

Use the method $.param jQuery to make the job easier. Also, rephrase the way to get the url.

Behold:

var parametro = $.param({
   fespecial: valor
});
var master = location.protocol + '//' + location.host + location.pathname + '?' + parametro

So you will mount your url according to the need :)

Another way to get the current url without the parameters may be done with the function split. In this case, you can use your code by only changing this section:

var url = location.href.split('?')[0] // pega o primeiro item gerado antes do "?"
  • That one $.param is almost an equivalent to http_build_query of PHP :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.