简体   繁体   English

如何根据下拉选择更新(附加到)href?

[英]how to update (append to it) an href based on dropdown selection?

How can append to an href link a value based on a dropdown selection? 如何将基于下拉选择的值附加到href链接?

Example : 范例:

<option value="100" selected="selected">MyList</option>

And a hyperlink like: 和一个超链接,如:

<a href="www.mysite.com/insert/place_where_insert_the_selected_value"></a>

So the link will be : 因此,链接将是:

<a href="www.mysite.com/insert/100"></a>

Thanks for your support 谢谢你的支持

I tried 我试过了

$('#IdMenuToEdit').change(function() {

    var id = $( "#IdMenuToEdit option:selected" ).val();
    var href = $('a.with-tip[name="DeleteMenu"]').attr("href");

    $('a.with-tip[name="DeleteMenu"]').attr('href',href+"/"+id);



});

it works but not very elegant 它有效但不是很优雅

Something like that: 像这样:

var valselected = $('select option:selected').val();
$('a').attr('href','www.mysite.com/insert/'+valselected);

Its something like that, i could give more complete answer if you give more complete code. 就像这样,如果您提供更完整的代码,我可以给出更完整的答案。 To be more precise this code need some "id" or "name" from the components like combobox and 为了更精确,此代码需要组合框和组合等组件的一些“ id”或“ name”

Try: 尝试:

change_link(); change_link();

$("select").change(function(){
    change_link();
});
function change_link(){
    var val = $("select option:selected").val();
    $("a").each(function(){
        var nhref = "";
        var href = $(this).attr("href").split("/");
        for(var i=0;i<href.length-1;i++){
            nhref += href[i]+"/";
        }
        nhref += val;
        $(this).attr("href",nhref);
    });
}

DEMO here. 演示在这里。

This would be a more efficient approach: 这将是一种更有效的方法:

let your html be like this: 让您的html像这样:

<select id="mycombo">
<option value="100" selected="selected">MyList</option>
... 
</select> 
 <a id="mylink" href="www.mysite.com/insert/"></a>

now, you capture combo's change event, then you change link href depending on the selected value. 现在,您捕获组合的更改事件,然后根据所选值更改链接href。 The first time, you save the base_href, to avoid to compute it each time. 第一次保存base_href,以避免每次都计算它。

var base_href="";
$('#mycombo').on('change', function() {
   var value=$(this).val();
    if (base_href=="")
        base_href = $("#mylink").attr("href");
   $("#mylink").attr("href",base_href+value);
 });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM