如何在href标签中获得选择框值

How to get select box value in a href tag

本文关键字:选择 href 标签      更新时间:2023-09-26

如何在php变量的href标签中获得选择框值?

<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>

代码如下所示,测试并100%工作,使用jQuery attr并给hyperlink一个ID,这里的ID是"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 
        });
    });

通过使用.attr()可以改变href<a>标签

change href on change select option

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

工作小提琴:http://jsfiddle.net/ehxj9zrx/