简体   繁体   中英

Redirect based on selected checkbox with JavaScript

I have several checkboxes in a switch statement like the one below:

function whatsChecked(obj) {
    var indx = obj.id.substring(obj.id.length-1, obj.id.length);
    switch ( indx ) {
        case '9':
            if (document.sport.soccer_9.checked) {
                //window.open.href = "../google.com";;
                window.open("../google.com"); 
            } 
            break;
        }
    }
}

How can I use a continue button to determine which checkbox was clicked and redirect me to the correct page (instead of giving each checkbox it's own onclick event)?

Also at http://jsfiddle.net/amelvin/YkFza/

In this example all the change events of the checkboxes register themselves, and if you check one of the boxes the value of the checkbox is alerted out. If you change the alert(this.value) to location.href(this.value) you will have your redirect.

<form>
Google: <input type="checkbox" name="Google" value="http://google.com" />
Yahoo: <input type="checkbox" name="Yahoo" value="http://yahoo.com" />
</form>  

<script>
function AddHandlers(f)
{
    var change_handler = new Function("alert(this.value)");

    for (var i=0; i<f.elements.length; i++)
     {
        var e = f.elements[i];
         e.onchange = change_handler;    
     }
}

AddHandlers(document.forms[0]);
</script>

I would make a variable, that switches depending on which checkbox is checked. (Perhaps an int value)

And then, when you click 'continue' you can say If (click == 1) { window.open(www.google.com") } or something to that effect. Probably not the most elegant solution, but it certainly should work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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