在选择电台,重定向到一个链接

on select radio, redirect to a link

本文关键字:一个 链接 选择 电台 重定向      更新时间:2023-09-26

我有三个无线电,我想要任何人的onselect被重定向到一个链接。使用javascript或jquery

    All <input name="EventRadio" type="radio" value="" checked="checked"/>&nbsp;
    Events<input name="EventRadio" type="radio" value="Events" />&nbsp;Classes<input name="EventRadio" type="radio" value="Classes"/><br /><br />

所以,因为"All"是默认选中的,我希望它去mysite.com/search.aspx。现在,如果用户选择Events,我想将用户重定向到mysite.com/search?type=Events或者如果用户选择Classes,我想将用户重定向到mysite.com/search?type=Classes作为对讲机开启的回应。我该如何做到这一点?

All     <input name="EventRadio" type="radio" value="" checked="checked" onclick ="goToLocation(this.value)"/>&nbsp;
Events  <input name="EventRadio" type="radio" value="Events" onclick ="goToLocation(this.value)"/>&nbsp;
Classes <input name="EventRadio" type="radio" value="Classes" onclick ="goToLocation(this.value)"/><br /><br />

function goToLocation(val){
 if(val == "Events")
     window.location = "go to Events location";
 if(val == "Classes")
     window.location = "go to Classes location";
window.location = "go to default location";
}

作为示范:

var inputs = document.getElementsByTagName('input'),
    radios = [],
    output = document.getElementById('output'),
    url = 'mysite.com/search?type=';
for (var i = 0, len = inputs.length; i<len; i++) {
    if (inputs[i].type == 'radio'){
        radios.push(inputs[i]);
    }
}
for (var r=0, leng = radios.length; r<leng; r++){
    radios[r].onchange = function(){
        if (this.value){
            /* in real life use:
            document.location = url + this.value;
            */
            output.innerHTML = url + this.value;
        }
        else {
            /* in real life use:
            document.location = 'mysite.com/search?type=Events';
            */
            output.innerHTML = 'mysite.com/search.aspx';
        }
    }
}

JS Fiddle demo.

请注意,我也改变了你的标记使用<label>元素,并删除了&nbsp; s和<br /> s。

$('input').click(function(){
    var val = $(this).val();
    if(val !== ''){
        window.location = 'http://mysite.com/search?type=' + val;
    }else{
        window.location = 'http://mysite.com/search.aspx';
    }
});