简体   繁体   中英

If form option value select equals then redirect PHP/jQuery

I have a form on my page with a dropdown of counties. I want to somehow, with jQuery or PHP say that if a certain county is selected then redirect otherwise process the form.

I've created a fiddle to try and explain...

http://jsfiddle.net/KVjAc/

Just submit the form and use a php header command for the values you want to redirect. That would be the first thing to check in the form processor script.

No need for javascript unless you are using ajax to refresh parts of the page based on the selection.

Do something like:

$(document).ready(function () {
    $('#county').on('change', function () {
        var value = $(this).val();
        if (value == 'cheshire' || value == 'city of london' || value == 'durham') {
            window.location.href = 'http://www.google.com/';
        }
    })
});

EDIT: Also, you need to add values to your option elements: <option value="cheshire">Cheshire</option>

I hope this helps(if you need a javascript/jquery solution),

$('#selectbox_ID').change( function() {
     if($(this).val() == "your country"){

     }
});

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