• 28 Novembre 2023 4:40 PM

JQuery: prendere valori GET dall’url e popolare un RADIO

Come prendere un valore GET presente nell’url e popolare un RADIO tramite JQuery

Nelle’esempio popoliamo  un RADIO che si chiama “Sistema Operativo”   e che ha due opzioni

function valuePass() {

function getParameterByName(name)
{
name = name.replace(/[\[]/, “\\\[“).replace(/[\]]/, “\\\]”);
var regexS = “[\\?&]” + name + “=([^&#]*)”;
var regex = new RegExp(regexS);
var results = regex.exec(window.location.search);
if(results == null)
return “”;
else
return decodeURIComponent(results[1].replace(/\+/g, ” “));
}

// Radio “Sistema Operativo”
var sistemaoperativo = getParameterByName(‘sistema-operativo’);

if (sistemaoperativo == “Windows”) {

$(‘input:radio[name=sistema-operativo][value=1]’).attr(‘checked’, true);

}  else if (sistemaoperativo == “Macintosh”) {

$(‘input:radio[name=sistema-operativo][value=2]’).attr(‘checked’, true);

}

}

valuePass();