6

I got option values from a query, and i added into dropdown list. here's the code:

$(document).ready(function() {
    $( "#uname" ).focusout(function() {
        $("#loader").show();
        var uname = $( "#uname" ).val(),
            v_request = $.ajax({
            url  : "data/get_agent.php",
            type : "POST",
            dataType: "json",
            data : {
                uname  : uname
            }
        });

        v_request.done(function(data, status, jqXHR) {
            $("#application").empty();
            var option = document.createElement("option"),
                select = document.getElementById("application");
                option.text = '-- Application --';
                option.value = '';
                select.appendChild(option);

            $.each(data.data, function(key, data) {
                var option = document.createElement("option"),
                    select = document.getElementById("application");
                option.text = data.appname;
                option.value = data.appvalue;
                select.appendChild(option);
                if(data.defvalue == 1)
                    select.value = data.appvalue;
            });
        });
    })
});

the option value contain url link, i want call it in a a href tag. here's code:

<tr>
    <td>
        <select id="application" name="application">
        <option value=""> -- Application --  </option>
        </select>
    </td>
</tr>
<tr>
    <td align="center" style="padding-top:10px;" id="logInBtn">
    <a id="link_combo" href="javascript:void(0);" onmouseout="MM_swapImgRestore()"><img src="resources/images/straclick-login.png" name="btn-login" border="0" width="280" id="btn-login" /></a>
    </td>
 </tr>

i don't know how to insert drop down value into href.

DimasW
  • 181
  • 1
  • 3
  • 11

2 Answers2

4

Just adding to Barmar's answer, here's an example: http://jsbin.com/panacoqaje/1/ (I would have commented but I don't yet have 50 reputation :P)

$("#application").change(function () {
  console.log(this.value);
  $("#link_combo").attr('href', this.value);
  $("#link_combo").text($("#application option:selected").text());
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<tr>
    <td>
        <select id="application">
        <option value="http://google.com/">Google</option>
        <option value="http://benzhang.xyz/">A Blog</option>
        </select>
    </td>
</tr>
<tr>
    <td align="center" style="padding-top:10px;" id="logInBtn">
      A link to: 
    <a id="link_combo" href="#">Unicorn Land</a>
    </td>
 </tr>
Ben
  • 1,557
  • 13
  • 30
  • Please make sure to give Barmar a thumbs up too, his answer is perfectly valid! – Ben May 05 '15 at 07:06
3

Use .val() to get the value of the dropdown when you click on the link.

$("#link_combo").click(function() {
    var url = $("#application").val();
    if (url != "") {
        window.location = url;
    }
});

Or when the user selects something from the dropdown, you can change the href:

$("#application").change() {
    var url = $(this).val();
    $("#link_combo").attr("href", url == "" ? "javascript:void(0)" : url);
});
Barmar
  • 741,623
  • 53
  • 500
  • 612