我从查询中获得了选项值,并将其添加到下拉列表中。这是代码:
/tmp/myrepo (master) $ touch newfile.txt
/tmp/myrepo (master %) $ git add newfile.txt
/tmp/myrepo (master +) $ git commit -m 'added newfile'
[master 9fe6399] added newfile
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 newfile.txt
/tmp/myrepo (master) $
选项值包含url链接,我想在一个href标签中调用它。这里的代码:
$(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;
});
});
})
});
我不知道如何将下拉值插入href。
答案 0 :(得分:3)
点击链接时,使用.val()
获取下拉列表的值。
$("#link_combo").click(function() {
var url = $("#application").val();
if (url != "") {
window.location = url;
}
});
或者当用户从下拉列表中选择内容时,您可以更改href
:
$("#application").change() {
var url = $(this).val();
$("#link_combo").attr("href", url == "" ? "javascript:void(0)" : url);
});
答案 1 :(得分:3)
只是添加Barmar的答案,这是一个例子:http://jsbin.com/panacoqaje/1/(我会评论但我还没有50个声望: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>