简体   繁体   中英

How to get select box value in a href tag

How do I get the select box value in a href tag with php variable?

<script type="text/javascript">
$(document).ready(function(){
        $("select.id").change(function(){
            var selectedid  = $(".id option:selected").val();
            alert(selectedid);
        });
    });
</script>

<label>Select Id</label>
<select class="id">
    <option value="">SELECT</option>
    <option value="1">1</option>
    <option value="2">2</option>
    <option value="3">3</option>
    <option value="4">4</option>
    <option value="4">5</option>
</select>

<a href="test.php?id=id">Testing</a>

Code shown below, tested and works 100%, use jQuery attr and give an ID to hyperlink, here the id is "link":

<a id="link" href="test.php?id=id">Testing</a>  <!-- GIVE AN ID, else will affect entire hyperlinks on your project -->

$(document).ready(function(){
        $("select.id").change(function(){

            var selectedid  = $(".id option:selected").val();
            $("#link").attr("href","test.php?id="+selectedid);  //-----this will change href 
        });
    });

by using .attr() you can change href of <a> tag

change href on change of select option

$(document).ready(function(){
        $("select.id").change(function(){
            var selectedid  = $(".id option:selected").val();
            $('a').attr('href','test.php?id='+selectedid);
        });
    });

Working Fiddle : http://jsfiddle.net/ehxj9zrx/

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